1 /* GStreamer
2 *
3 * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <gst/rtp/gstrtpbuffer.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include "gstrtpg723depay.h"
30
31 GST_DEBUG_CATEGORY_STATIC (rtpg723depay_debug);
32 #define GST_CAT_DEFAULT (rtpg723depay_debug)
33
34
35 /* references:
36 *
37 * RFC 3551 (4.5.3)
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_g723_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) \"G723\"; "
62 "application/x-rtp, "
63 "media = (string) \"audio\", "
64 "payload = (int) " GST_RTP_PAYLOAD_G723_STRING ", "
65 "clock-rate = (int) 8000")
66 );
67
68 static GstStaticPadTemplate gst_rtp_g723_depay_src_template =
69 GST_STATIC_PAD_TEMPLATE ("src",
70 GST_PAD_SRC,
71 GST_PAD_ALWAYS,
72 GST_STATIC_CAPS ("audio/G723, " "channels = (int) 1," "rate = (int) 8000")
73 );
74
75 static gboolean gst_rtp_g723_depay_setcaps (GstRTPBaseDepayload * depayload,
76 GstCaps * caps);
77 static GstBuffer *gst_rtp_g723_depay_process (GstRTPBaseDepayload * depayload,
78 GstRTPBuffer * rtp);
79
80 #define gst_rtp_g723_depay_parent_class parent_class
81 G_DEFINE_TYPE (GstRtpG723Depay, gst_rtp_g723_depay,
82 GST_TYPE_RTP_BASE_DEPAYLOAD);
83
84 static void
gst_rtp_g723_depay_class_init(GstRtpG723DepayClass * klass)85 gst_rtp_g723_depay_class_init (GstRtpG723DepayClass * klass)
86 {
87 GstElementClass *gstelement_class;
88 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
89
90 GST_DEBUG_CATEGORY_INIT (rtpg723depay_debug, "rtpg723depay", 0,
91 "G.723 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_g723_depay_src_template);
98 gst_element_class_add_static_pad_template (gstelement_class,
99 &gst_rtp_g723_depay_sink_template);
100
101 gst_element_class_set_static_metadata (gstelement_class,
102 "RTP G.723 depayloader", "Codec/Depayloader/Network/RTP",
103 "Extracts G.723 audio from RTP packets (RFC 3551)",
104 "Wim Taymans <wim.taymans@gmail.com>");
105
106 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_g723_depay_process;
107 gstrtpbasedepayload_class->set_caps = gst_rtp_g723_depay_setcaps;
108 }
109
110 static void
gst_rtp_g723_depay_init(GstRtpG723Depay * rtpg723depay)111 gst_rtp_g723_depay_init (GstRtpG723Depay * rtpg723depay)
112 {
113 GstRTPBaseDepayload *depayload;
114
115 depayload = GST_RTP_BASE_DEPAYLOAD (rtpg723depay);
116
117 gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
118 }
119
120 static gboolean
gst_rtp_g723_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)121 gst_rtp_g723_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
122 {
123 GstStructure *structure;
124 GstCaps *srccaps;
125 GstRtpG723Depay *rtpg723depay;
126 const gchar *params;
127 gint clock_rate, channels;
128 gboolean ret;
129
130 rtpg723depay = GST_RTP_G723_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/G723",
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 (rtpg723depay, "expected 1 channel, got %d", channels);
162 return FALSE;
163 }
164 wrong_clock_rate:
165 {
166 GST_DEBUG_OBJECT (rtpg723depay, "expected 8000 clock-rate, got %d",
167 clock_rate);
168 return FALSE;
169 }
170 }
171
172
173 static GstBuffer *
gst_rtp_g723_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)174 gst_rtp_g723_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
175 {
176 GstRtpG723Depay *rtpg723depay;
177 GstBuffer *outbuf = NULL;
178 gint payload_len;
179 gboolean marker;
180
181 rtpg723depay = GST_RTP_G723_DEPAY (depayload);
182
183 payload_len = gst_rtp_buffer_get_payload_len (rtp);
184
185 /* At least 4 bytes */
186 if (payload_len < 4)
187 goto too_small;
188
189 GST_LOG_OBJECT (rtpg723depay, "payload len %d", payload_len);
190
191 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
192 marker = gst_rtp_buffer_get_marker (rtp);
193
194 if (marker) {
195 /* marker bit starts talkspurt */
196 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
197 }
198
199 GST_LOG_OBJECT (depayload, "pushing buffer of size %" G_GSIZE_FORMAT,
200 gst_buffer_get_size (outbuf));
201
202 return outbuf;
203
204 /* ERRORS */
205 too_small:
206 {
207 GST_ELEMENT_WARNING (rtpg723depay, STREAM, DECODE,
208 (NULL), ("G723 RTP payload too small (%d)", payload_len));
209 goto bad_packet;
210 }
211 bad_packet:
212 {
213 /* no fatal error */
214 return NULL;
215 }
216 }
217
218 gboolean
gst_rtp_g723_depay_plugin_init(GstPlugin * plugin)219 gst_rtp_g723_depay_plugin_init (GstPlugin * plugin)
220 {
221 return gst_element_register (plugin, "rtpg723depay",
222 GST_RANK_SECONDARY, GST_TYPE_RTP_G723_DEPAY);
223 }
224