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