1 /* GStreamer
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
12 *
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include <gst/rtp/gstrtpbuffer.h>
24 #include <gst/audio/audio.h>
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include "gstrtpg729depay.h"
29 #include "gstrtputils.h"
30
31 GST_DEBUG_CATEGORY_STATIC (rtpg729depay_debug);
32 #define GST_CAT_DEFAULT (rtpg729depay_debug)
33
34
35 /* references:
36 *
37 * RFC 3551 (4.5.6)
38 */
39
40 enum
41 {
42 /* FILL ME */
43 LAST_SIGNAL
44 };
45
46 enum
47 {
48 PROP_0
49 };
50
51 /* input is an RTP packet
52 *
53 */
54 static GstStaticPadTemplate gst_rtp_g729_depay_sink_template =
55 GST_STATIC_PAD_TEMPLATE ("sink",
56 GST_PAD_SINK,
57 GST_PAD_ALWAYS,
58 GST_STATIC_CAPS ("application/x-rtp, "
59 "media = (string) \"audio\", "
60 "clock-rate = (int) 8000, "
61 "encoding-name = (string) \"G729\"; "
62 "application/x-rtp, "
63 "media = (string) \"audio\", "
64 "payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
65 "clock-rate = (int) 8000")
66 );
67
68 static GstStaticPadTemplate gst_rtp_g729_depay_src_template =
69 GST_STATIC_PAD_TEMPLATE ("src",
70 GST_PAD_SRC,
71 GST_PAD_ALWAYS,
72 GST_STATIC_CAPS ("audio/G729, " "channels = (int) 1," "rate = (int) 8000")
73 );
74
75 static gboolean gst_rtp_g729_depay_setcaps (GstRTPBaseDepayload * depayload,
76 GstCaps * caps);
77 static GstBuffer *gst_rtp_g729_depay_process (GstRTPBaseDepayload * depayload,
78 GstRTPBuffer * rtp);
79
80 #define gst_rtp_g729_depay_parent_class parent_class
81 G_DEFINE_TYPE (GstRtpG729Depay, gst_rtp_g729_depay,
82 GST_TYPE_RTP_BASE_DEPAYLOAD);
83
84 static void
gst_rtp_g729_depay_class_init(GstRtpG729DepayClass * klass)85 gst_rtp_g729_depay_class_init (GstRtpG729DepayClass * klass)
86 {
87 GstElementClass *gstelement_class;
88 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
89
90 GST_DEBUG_CATEGORY_INIT (rtpg729depay_debug, "rtpg729depay", 0,
91 "G.729 RTP Depayloader");
92
93 gstelement_class = (GstElementClass *) klass;
94 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
95
96 gst_element_class_add_static_pad_template (gstelement_class,
97 &gst_rtp_g729_depay_src_template);
98 gst_element_class_add_static_pad_template (gstelement_class,
99 &gst_rtp_g729_depay_sink_template);
100
101 gst_element_class_set_static_metadata (gstelement_class,
102 "RTP G.729 depayloader", "Codec/Depayloader/Network/RTP",
103 "Extracts G.729 audio from RTP packets (RFC 3551)",
104 "Laurent Glayal <spglegle@yahoo.fr>");
105
106 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_g729_depay_process;
107 gstrtpbasedepayload_class->set_caps = gst_rtp_g729_depay_setcaps;
108 }
109
110 static void
gst_rtp_g729_depay_init(GstRtpG729Depay * rtpg729depay)111 gst_rtp_g729_depay_init (GstRtpG729Depay * rtpg729depay)
112 {
113 GstRTPBaseDepayload *depayload;
114
115 depayload = GST_RTP_BASE_DEPAYLOAD (rtpg729depay);
116
117 gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
118 }
119
120 static gboolean
gst_rtp_g729_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)121 gst_rtp_g729_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
122 {
123 GstStructure *structure;
124 GstCaps *srccaps;
125 GstRtpG729Depay *rtpg729depay;
126 const gchar *params;
127 gint clock_rate, channels;
128 gboolean ret;
129
130 rtpg729depay = GST_RTP_G729_DEPAY (depayload);
131
132 structure = gst_caps_get_structure (caps, 0);
133
134 if (!(params = gst_structure_get_string (structure, "encoding-params")))
135 channels = 1;
136 else {
137 channels = atoi (params);
138 }
139
140 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
141 clock_rate = 8000;
142
143 if (channels != 1)
144 goto wrong_channels;
145
146 if (clock_rate != 8000)
147 goto wrong_clock_rate;
148
149 depayload->clock_rate = clock_rate;
150
151 srccaps = gst_caps_new_simple ("audio/G729",
152 "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, clock_rate, NULL);
153 ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
154 gst_caps_unref (srccaps);
155
156 return ret;
157
158 /* ERRORS */
159 wrong_channels:
160 {
161 GST_DEBUG_OBJECT (rtpg729depay, "expected 1 channel, got %d", channels);
162 return FALSE;
163 }
164 wrong_clock_rate:
165 {
166 GST_DEBUG_OBJECT (rtpg729depay, "expected 8000 clock-rate, got %d",
167 clock_rate);
168 return FALSE;
169 }
170 }
171
172 static GstBuffer *
gst_rtp_g729_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)173 gst_rtp_g729_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
174 {
175 GstRtpG729Depay *rtpg729depay;
176 GstBuffer *outbuf = NULL;
177 gint payload_len;
178 gboolean marker;
179
180 rtpg729depay = GST_RTP_G729_DEPAY (depayload);
181
182 payload_len = gst_rtp_buffer_get_payload_len (rtp);
183
184 /* At least 2 bytes (CNG from G729 Annex B) */
185 if (payload_len < 2) {
186 GST_ELEMENT_WARNING (rtpg729depay, STREAM, DECODE,
187 (NULL), ("G729 RTP payload too small (%d)", payload_len));
188 goto bad_packet;
189 }
190
191 GST_LOG_OBJECT (rtpg729depay, "payload len %d", payload_len);
192
193 if ((payload_len % 10) == 2) {
194 GST_LOG_OBJECT (rtpg729depay, "G729 payload contains CNG frame");
195 }
196
197 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
198 marker = gst_rtp_buffer_get_marker (rtp);
199
200 if (marker) {
201 /* marker bit starts talkspurt */
202 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
203 }
204
205 gst_rtp_drop_non_audio_meta (depayload, outbuf);
206
207 GST_LOG_OBJECT (depayload, "pushing buffer of size %" G_GSIZE_FORMAT,
208 gst_buffer_get_size (outbuf));
209
210 return outbuf;
211
212 /* ERRORS */
213 bad_packet:
214 {
215 /* no fatal error */
216 return NULL;
217 }
218 }
219
220 gboolean
gst_rtp_g729_depay_plugin_init(GstPlugin * plugin)221 gst_rtp_g729_depay_plugin_init (GstPlugin * plugin)
222 {
223 return gst_element_register (plugin, "rtpg729depay",
224 GST_RANK_SECONDARY, GST_TYPE_RTP_G729_DEPAY);
225 }
226