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