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