gst/rtp/gstrtpL16depay.c: Check if clock-rate and channels are valid.
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpg729depay.c
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., 59 Temple Place - Suite 330,
16  * Boston, MA 02111-1307, USA.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #endif
22
23 #include <gst/rtp/gstrtpbuffer.h>
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include "gstrtpg729depay.h"
28
29 /* references:
30  *
31  * RFC 3551 (4.5.6)
32  */
33
34 /* elementfactory information */
35 static const GstElementDetails gst_rtp_g729depay_details =
36 GST_ELEMENT_DETAILS ("RTP packet depayloader",
37     "Codec/Depayloader/Network",
38     "Extracts G729 audio from RTP packets (RFC 3551)",
39     "Laurent Glayal <spglegle@yahoo.fr>");
40
41 enum
42 {
43   /* FILL ME */
44   LAST_SIGNAL
45 };
46
47 enum
48 {
49   ARG_0
50 };
51
52 /* input is an RTP packet
53  *
54  */
55 static GstStaticPadTemplate gst_rtp_g729_depay_sink_template =
56     GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp, "
60         "media = (string) \"audio\", "
61         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
62         "clock-rate = (int) 8000, "
63         "encoding-name = (string) \"G729\"; "
64         "application/x-rtp, "
65         "media = (string) \"audio\", "
66         "payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
67         "clock-rate = (int) 8000")
68     );
69
70 static GstStaticPadTemplate gst_rtp_g729_depay_src_template =
71 GST_STATIC_PAD_TEMPLATE ("src",
72     GST_PAD_SRC,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS ("audio/G729, " "channels = (int) 1," "rate = (int) 8000")
75     );
76
77 static gboolean gst_rtp_g729_depay_setcaps (GstBaseRTPDepayload * depayload,
78     GstCaps * caps);
79 static GstBuffer *gst_rtp_g729_depay_process (GstBaseRTPDepayload * depayload,
80     GstBuffer * buf);
81
82 GST_BOILERPLATE (GstRtpG729Depay, gst_rtp_g729_depay, GstBaseRTPDepayload,
83     GST_TYPE_BASE_RTP_DEPAYLOAD);
84
85 static void
86 gst_rtp_g729_depay_base_init (gpointer klass)
87 {
88   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
89
90   gst_element_class_add_pad_template (element_class,
91       gst_static_pad_template_get (&gst_rtp_g729_depay_src_template));
92   gst_element_class_add_pad_template (element_class,
93       gst_static_pad_template_get (&gst_rtp_g729_depay_sink_template));
94
95   gst_element_class_set_details (element_class, &gst_rtp_g729depay_details);
96 }
97
98 static void
99 gst_rtp_g729_depay_class_init (GstRtpG729DepayClass * klass)
100 {
101   GObjectClass *gobject_class;
102   GstElementClass *gstelement_class;
103   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
104
105   gobject_class = (GObjectClass *) klass;
106   gstelement_class = (GstElementClass *) klass;
107   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
108
109   parent_class = g_type_class_peek_parent (klass);
110
111   gstbasertpdepayload_class->process = gst_rtp_g729_depay_process;
112   gstbasertpdepayload_class->set_caps = gst_rtp_g729_depay_setcaps;
113 }
114
115 static void
116 gst_rtp_g729_depay_init (GstRtpG729Depay * rtpg729depay,
117     GstRtpG729DepayClass * klass)
118 {
119   GstBaseRTPDepayload *depayload;
120
121   depayload = GST_BASE_RTP_DEPAYLOAD (rtpg729depay);
122
123   gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
124 }
125
126 static gboolean
127 gst_rtp_g729_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
128 {
129   GstStructure *structure;
130   GstCaps *srccaps;
131   GstRtpG729Depay *rtpg729depay;
132   const gchar *params;
133   gint clock_rate, channels;
134   gboolean ret;
135
136   rtpg729depay = GST_RTP_G729_DEPAY (depayload);
137
138   structure = gst_caps_get_structure (caps, 0);
139
140   if (!(params = gst_structure_get_string (structure, "encoding-params")))
141     channels = 1;
142   else {
143     channels = atoi (params);
144   }
145
146   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
147     clock_rate = 8000;
148
149   if (channels != 1)
150     goto wrong_channels;
151
152   if (clock_rate != 8000)
153     goto wrong_clock_rate;
154
155   depayload->clock_rate = clock_rate;
156
157   srccaps = gst_caps_new_simple ("audio/G729",
158       "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, clock_rate, NULL);
159   ret = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
160   gst_caps_unref (srccaps);
161
162   return ret;
163
164   /* ERRORS */
165 wrong_channels:
166   {
167     GST_DEBUG_OBJECT (rtpg729depay, "expected 1 channel, got %d",
168         rtpg729depay->channels);
169     return FALSE;
170   }
171 wrong_clock_rate:
172   {
173     GST_DEBUG_OBJECT (rtpg729depay, "expected 8000 clock-rate, got %d",
174         clock_rate);
175     return FALSE;
176   }
177 }
178
179
180 static GstBuffer *
181 gst_rtp_g729_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
182 {
183   GstRtpG729Depay *rtpg729depay;
184   GstBuffer *outbuf = NULL;
185   gint payload_len;
186   gboolean marker;
187
188   rtpg729depay = GST_RTP_G729_DEPAY (depayload);
189
190   payload_len = gst_rtp_buffer_get_payload_len (buf);
191
192   /* At least 2 bytes (CNG from G729 Annex B) */
193   if (payload_len < 2) {
194     GST_ELEMENT_WARNING (rtpg729depay, STREAM, DECODE,
195         (NULL), ("G729 RTP payload too small (%d)", payload_len));
196     goto bad_packet;
197   }
198
199   GST_DEBUG_OBJECT (rtpg729depay, "payload len %d", payload_len);
200
201   if ((payload_len % 10) == 2) {
202     GST_DEBUG_OBJECT (rtpg729depay, "G729 payload contains CNG frame");
203   }
204
205   outbuf = gst_rtp_buffer_get_payload_buffer (buf);
206   marker = gst_rtp_buffer_get_marker (buf);
207
208   if (marker) {
209     /* marker bit starts talkspurt */
210     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
211   }
212
213   GST_DEBUG ("gst_rtp_g729_depay_chain: pushing buffer of size %d",
214       GST_BUFFER_SIZE (outbuf));
215
216   return outbuf;
217
218   /* ERRORS */
219 bad_packet:
220   {
221     /* no fatal error */
222     return NULL;
223   }
224 }
225
226 gboolean
227 gst_rtp_g729_depay_plugin_init (GstPlugin * plugin)
228 {
229   return gst_element_register (plugin, "rtpg729depay",
230       GST_RANK_MARGINAL, GST_TYPE_RTP_G729_DEPAY);
231 }