rtpvp8: port some more to new memory API
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpg723depay.c
1 /* GStreamer
2  *
3  * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <gst/rtp/gstrtpbuffer.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include "gstrtpg723depay.h"
30
31 GST_DEBUG_CATEGORY_STATIC (rtpg723depay_debug);
32 #define GST_CAT_DEFAULT (rtpg723depay_debug)
33
34
35 /* references:
36  *
37  * RFC 3551 (4.5.3)
38  */
39
40 enum
41 {
42   /* FILL ME */
43   LAST_SIGNAL
44 };
45
46 enum
47 {
48   ARG_0
49 };
50
51 /* input is an RTP packet
52  *
53  */
54 static GstStaticPadTemplate gst_rtp_g723_depay_sink_template =
55     GST_STATIC_PAD_TEMPLATE ("sink",
56     GST_PAD_SINK,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS ("application/x-rtp, "
59         "media = (string) \"audio\", "
60         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
61         "clock-rate = (int) 8000, "
62         "encoding-name = (string) \"G723\"; "
63         "application/x-rtp, "
64         "media = (string) \"audio\", "
65         "payload = (int) " GST_RTP_PAYLOAD_G723_STRING ", "
66         "clock-rate = (int) 8000")
67     );
68
69 static GstStaticPadTemplate gst_rtp_g723_depay_src_template =
70 GST_STATIC_PAD_TEMPLATE ("src",
71     GST_PAD_SRC,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("audio/G723, " "channels = (int) 1," "rate = (int) 8000")
74     );
75
76 static gboolean gst_rtp_g723_depay_setcaps (GstRTPBaseDepayload * depayload,
77     GstCaps * caps);
78 static GstBuffer *gst_rtp_g723_depay_process (GstRTPBaseDepayload * depayload,
79     GstBuffer * buf);
80
81 #define gst_rtp_g723_depay_parent_class parent_class
82 G_DEFINE_TYPE (GstRtpG723Depay, gst_rtp_g723_depay,
83     GST_TYPE_RTP_BASE_DEPAYLOAD);
84
85 static void
86 gst_rtp_g723_depay_class_init (GstRtpG723DepayClass * klass)
87 {
88   GstElementClass *gstelement_class;
89   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
90
91   GST_DEBUG_CATEGORY_INIT (rtpg723depay_debug, "rtpg723depay", 0,
92       "G.723 RTP Depayloader");
93
94   gstelement_class = (GstElementClass *) klass;
95   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
96
97   gst_element_class_add_pad_template (gstelement_class,
98       gst_static_pad_template_get (&gst_rtp_g723_depay_src_template));
99   gst_element_class_add_pad_template (gstelement_class,
100       gst_static_pad_template_get (&gst_rtp_g723_depay_sink_template));
101
102   gst_element_class_set_static_metadata (gstelement_class,
103       "RTP G.723 depayloader", "Codec/Depayloader/Network/RTP",
104       "Extracts G.723 audio from RTP packets (RFC 3551)",
105       "Wim Taymans <wim.taymans@gmail.com>");
106
107   gstrtpbasedepayload_class->process = gst_rtp_g723_depay_process;
108   gstrtpbasedepayload_class->set_caps = gst_rtp_g723_depay_setcaps;
109 }
110
111 static void
112 gst_rtp_g723_depay_init (GstRtpG723Depay * rtpg723depay)
113 {
114   GstRTPBaseDepayload *depayload;
115
116   depayload = GST_RTP_BASE_DEPAYLOAD (rtpg723depay);
117
118   gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
119 }
120
121 static gboolean
122 gst_rtp_g723_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
123 {
124   GstStructure *structure;
125   GstCaps *srccaps;
126   GstRtpG723Depay *rtpg723depay;
127   const gchar *params;
128   gint clock_rate, channels;
129   gboolean ret;
130
131   rtpg723depay = GST_RTP_G723_DEPAY (depayload);
132
133   structure = gst_caps_get_structure (caps, 0);
134
135   if (!(params = gst_structure_get_string (structure, "encoding-params")))
136     channels = 1;
137   else {
138     channels = atoi (params);
139   }
140
141   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
142     clock_rate = 8000;
143
144   if (channels != 1)
145     goto wrong_channels;
146
147   if (clock_rate != 8000)
148     goto wrong_clock_rate;
149
150   depayload->clock_rate = clock_rate;
151
152   srccaps = gst_caps_new_simple ("audio/G723",
153       "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, clock_rate, NULL);
154   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
155   gst_caps_unref (srccaps);
156
157   return ret;
158
159   /* ERRORS */
160 wrong_channels:
161   {
162     GST_DEBUG_OBJECT (rtpg723depay, "expected 1 channel, got %d", channels);
163     return FALSE;
164   }
165 wrong_clock_rate:
166   {
167     GST_DEBUG_OBJECT (rtpg723depay, "expected 8000 clock-rate, got %d",
168         clock_rate);
169     return FALSE;
170   }
171 }
172
173
174 static GstBuffer *
175 gst_rtp_g723_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
176 {
177   GstRtpG723Depay *rtpg723depay;
178   GstBuffer *outbuf = NULL;
179   gint payload_len;
180   gboolean marker;
181   GstRTPBuffer rtp = { NULL };
182
183   rtpg723depay = GST_RTP_G723_DEPAY (depayload);
184
185   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
186
187   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
188
189   /* At least 4 bytes */
190   if (payload_len < 4)
191     goto too_small;
192
193   GST_LOG_OBJECT (rtpg723depay, "payload len %d", payload_len);
194
195   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
196   marker = gst_rtp_buffer_get_marker (&rtp);
197   gst_rtp_buffer_unmap (&rtp);
198
199   if (marker) {
200     /* marker bit starts talkspurt */
201     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
202   }
203
204   GST_LOG_OBJECT (depayload, "pushing buffer of size %" G_GSIZE_FORMAT,
205       gst_buffer_get_size (outbuf));
206
207   return outbuf;
208
209   /* ERRORS */
210 too_small:
211   {
212     GST_ELEMENT_WARNING (rtpg723depay, STREAM, DECODE,
213         (NULL), ("G723 RTP payload too small (%d)", payload_len));
214     goto bad_packet;
215   }
216 bad_packet:
217   {
218     /* no fatal error */
219     gst_rtp_buffer_unmap (&rtp);
220     return NULL;
221   }
222 }
223
224 gboolean
225 gst_rtp_g723_depay_plugin_init (GstPlugin * plugin)
226 {
227   return gst_element_register (plugin, "rtpg723depay",
228       GST_RANK_SECONDARY, GST_TYPE_RTP_G723_DEPAY);
229 }