tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.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 GST_BOILERPLATE (GstRtpG729Depay, gst_rtp_g729_depay, GstBaseRTPDepayload,
80     GST_TYPE_BASE_RTP_DEPAYLOAD);
81
82 static void
83 gst_rtp_g729_depay_base_init (gpointer klass)
84 {
85   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
86
87   gst_element_class_add_static_pad_template (element_class,
88       &gst_rtp_g729_depay_src_template);
89   gst_element_class_add_static_pad_template (element_class,
90       &gst_rtp_g729_depay_sink_template);
91
92   gst_element_class_set_details_simple (element_class, "RTP G.729 depayloader",
93       "Codec/Depayloader/Network/RTP",
94       "Extracts G.729 audio from RTP packets (RFC 3551)",
95       "Laurent Glayal <spglegle@yahoo.fr>");
96
97   GST_DEBUG_CATEGORY_INIT (rtpg729depay_debug, "rtpg729depay", 0,
98       "G.729 RTP Depayloader");
99 }
100
101 static void
102 gst_rtp_g729_depay_class_init (GstRtpG729DepayClass * klass)
103 {
104   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
105
106   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
107
108   gstbasertpdepayload_class->process = gst_rtp_g729_depay_process;
109   gstbasertpdepayload_class->set_caps = gst_rtp_g729_depay_setcaps;
110 }
111
112 static void
113 gst_rtp_g729_depay_init (GstRtpG729Depay * rtpg729depay,
114     GstRtpG729DepayClass * klass)
115 {
116   GstBaseRTPDepayload *depayload;
117
118   depayload = GST_BASE_RTP_DEPAYLOAD (rtpg729depay);
119
120   gst_pad_use_fixed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload));
121 }
122
123 static gboolean
124 gst_rtp_g729_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
125 {
126   GstStructure *structure;
127   GstCaps *srccaps;
128   GstRtpG729Depay *rtpg729depay;
129   const gchar *params;
130   gint clock_rate, channels;
131   gboolean ret;
132
133   rtpg729depay = GST_RTP_G729_DEPAY (depayload);
134
135   structure = gst_caps_get_structure (caps, 0);
136
137   if (!(params = gst_structure_get_string (structure, "encoding-params")))
138     channels = 1;
139   else {
140     channels = atoi (params);
141   }
142
143   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
144     clock_rate = 8000;
145
146   if (channels != 1)
147     goto wrong_channels;
148
149   if (clock_rate != 8000)
150     goto wrong_clock_rate;
151
152   depayload->clock_rate = clock_rate;
153
154   srccaps = gst_caps_new_simple ("audio/G729",
155       "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, clock_rate, NULL);
156   ret = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
157   gst_caps_unref (srccaps);
158
159   return ret;
160
161   /* ERRORS */
162 wrong_channels:
163   {
164     GST_DEBUG_OBJECT (rtpg729depay, "expected 1 channel, got %d", channels);
165     return FALSE;
166   }
167 wrong_clock_rate:
168   {
169     GST_DEBUG_OBJECT (rtpg729depay, "expected 8000 clock-rate, got %d",
170         clock_rate);
171     return FALSE;
172   }
173 }
174
175
176 static GstBuffer *
177 gst_rtp_g729_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
178 {
179   GstRtpG729Depay *rtpg729depay;
180   GstBuffer *outbuf = NULL;
181   gint payload_len;
182   gboolean marker;
183
184   rtpg729depay = GST_RTP_G729_DEPAY (depayload);
185
186   payload_len = gst_rtp_buffer_get_payload_len (buf);
187
188   /* At least 2 bytes (CNG from G729 Annex B) */
189   if (payload_len < 2) {
190     GST_ELEMENT_WARNING (rtpg729depay, STREAM, DECODE,
191         (NULL), ("G729 RTP payload too small (%d)", payload_len));
192     goto bad_packet;
193   }
194
195   GST_LOG_OBJECT (rtpg729depay, "payload len %d", payload_len);
196
197   if ((payload_len % 10) == 2) {
198     GST_LOG_OBJECT (rtpg729depay, "G729 payload contains CNG frame");
199   }
200
201   outbuf = gst_rtp_buffer_get_payload_buffer (buf);
202   marker = gst_rtp_buffer_get_marker (buf);
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_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 }