gstdepay: check for correct fragment offset
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpg722depay.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gst/audio/audio.h>
28
29 #include "gstrtpg722depay.h"
30 #include "gstrtpchannels.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpg722depay_debug);
33 #define GST_CAT_DEFAULT (rtpg722depay_debug)
34
35 static GstStaticPadTemplate gst_rtp_g722_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
37     GST_PAD_SRC,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("audio/G722, "
40         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
41     );
42
43 static GstStaticPadTemplate gst_rtp_g722_depay_sink_template =
44     GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("application/x-rtp, "
48         "media = (string) \"audio\", "
49         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
50         "clock-rate = (int) 8000, "
51         /* "channels = (int) [1, MAX]"  */
52         /* "channel-order = (string) ANY" */
53         "encoding-name = (string) \"G722\";"
54         "application/x-rtp, "
55         "media = (string) \"audio\", "
56         "payload = (int) " GST_RTP_PAYLOAD_G722_STRING ", "
57         "clock-rate = (int) [ 1, MAX ]"
58         /* "channels = (int) [1, MAX]" */
59         /* "emphasis = (string) ANY" */
60         /* "channel-order = (string) ANY" */
61     )
62     );
63
64 #define gst_rtp_g722_depay_parent_class parent_class
65 G_DEFINE_TYPE (GstRtpG722Depay, gst_rtp_g722_depay,
66     GST_TYPE_RTP_BASE_DEPAYLOAD);
67
68 static gboolean gst_rtp_g722_depay_setcaps (GstRTPBaseDepayload * depayload,
69     GstCaps * caps);
70 static GstBuffer *gst_rtp_g722_depay_process (GstRTPBaseDepayload * depayload,
71     GstBuffer * buf);
72
73 static void
74 gst_rtp_g722_depay_class_init (GstRtpG722DepayClass * klass)
75 {
76   GstElementClass *gstelement_class;
77   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
78
79   GST_DEBUG_CATEGORY_INIT (rtpg722depay_debug, "rtpg722depay", 0,
80       "G722 RTP Depayloader");
81
82   gstelement_class = (GstElementClass *) klass;
83   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
84
85   gst_element_class_add_pad_template (gstelement_class,
86       gst_static_pad_template_get (&gst_rtp_g722_depay_src_template));
87   gst_element_class_add_pad_template (gstelement_class,
88       gst_static_pad_template_get (&gst_rtp_g722_depay_sink_template));
89
90   gst_element_class_set_static_metadata (gstelement_class,
91       "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
92       "Extracts G722 audio from RTP packets",
93       "Wim Taymans <wim.taymans@gmail.com>");
94
95   gstrtpbasedepayload_class->set_caps = gst_rtp_g722_depay_setcaps;
96   gstrtpbasedepayload_class->process = gst_rtp_g722_depay_process;
97 }
98
99 static void
100 gst_rtp_g722_depay_init (GstRtpG722Depay * rtpg722depay)
101 {
102 }
103
104 static gint
105 gst_rtp_g722_depay_parse_int (GstStructure * structure, const gchar * field,
106     gint def)
107 {
108   const gchar *str;
109   gint res;
110
111   if ((str = gst_structure_get_string (structure, field)))
112     return atoi (str);
113
114   if (gst_structure_get_int (structure, field, &res))
115     return res;
116
117   return def;
118 }
119
120 static gboolean
121 gst_rtp_g722_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
122 {
123   GstStructure *structure;
124   GstRtpG722Depay *rtpg722depay;
125   gint clock_rate, payload, samplerate;
126   gint channels;
127   GstCaps *srccaps;
128   gboolean res;
129 #if 0
130   const gchar *channel_order;
131   const GstRTPChannelOrder *order;
132 #endif
133
134   rtpg722depay = GST_RTP_G722_DEPAY (depayload);
135
136   structure = gst_caps_get_structure (caps, 0);
137
138   payload = 96;
139   gst_structure_get_int (structure, "payload", &payload);
140   switch (payload) {
141     case GST_RTP_PAYLOAD_G722:
142       channels = 1;
143       clock_rate = 8000;
144       samplerate = 16000;
145       break;
146     default:
147       /* no fixed mapping, we need clock-rate */
148       channels = 0;
149       clock_rate = 0;
150       samplerate = 0;
151       break;
152   }
153
154   /* caps can overwrite defaults */
155   clock_rate =
156       gst_rtp_g722_depay_parse_int (structure, "clock-rate", clock_rate);
157   if (clock_rate == 0)
158     goto no_clockrate;
159
160   if (clock_rate == 8000)
161     samplerate = 16000;
162
163   if (samplerate == 0)
164     samplerate = clock_rate;
165
166   channels =
167       gst_rtp_g722_depay_parse_int (structure, "encoding-params", channels);
168   if (channels == 0) {
169     channels = gst_rtp_g722_depay_parse_int (structure, "channels", channels);
170     if (channels == 0) {
171       /* channels defaults to 1 otherwise */
172       channels = 1;
173     }
174   }
175
176   depayload->clock_rate = clock_rate;
177   rtpg722depay->rate = samplerate;
178   rtpg722depay->channels = channels;
179
180   srccaps = gst_caps_new_simple ("audio/G722",
181       "rate", G_TYPE_INT, samplerate, "channels", G_TYPE_INT, channels, NULL);
182
183   /* FIXME: Do something with the channel order */
184 #if 0
185   /* add channel positions */
186   channel_order = gst_structure_get_string (structure, "channel-order");
187
188   order = gst_rtp_channels_get_by_order (channels, channel_order);
189   if (order) {
190     gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0),
191         order->pos);
192   } else {
193     GstAudioChannelPosition *pos;
194
195     GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
196         (NULL), ("Unknown channel order '%s' for %d channels",
197             GST_STR_NULL (channel_order), channels));
198     /* create default NONE layout */
199     pos = gst_rtp_channels_create_default (channels);
200     gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0), pos);
201     g_free (pos);
202   }
203 #endif
204
205   res = gst_pad_set_caps (depayload->srcpad, srccaps);
206   gst_caps_unref (srccaps);
207
208   return res;
209
210   /* ERRORS */
211 no_clockrate:
212   {
213     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
214     return FALSE;
215   }
216 }
217
218 static GstBuffer *
219 gst_rtp_g722_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
220 {
221   GstRtpG722Depay *rtpg722depay;
222   GstBuffer *outbuf;
223   gint payload_len;
224   gboolean marker;
225   GstRTPBuffer rtp = { NULL };
226
227   rtpg722depay = GST_RTP_G722_DEPAY (depayload);
228
229   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
230
231   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
232
233   if (payload_len <= 0)
234     goto empty_packet;
235
236   GST_DEBUG_OBJECT (rtpg722depay, "got payload of %d bytes", payload_len);
237
238   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
239   marker = gst_rtp_buffer_get_marker (&rtp);
240   gst_rtp_buffer_unmap (&rtp);
241
242   if (marker && outbuf) {
243     /* mark talk spurt with DISCONT */
244     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
245   }
246
247   return outbuf;
248
249   /* ERRORS */
250 empty_packet:
251   {
252     GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
253         ("Empty Payload."), (NULL));
254     gst_rtp_buffer_unmap (&rtp);
255     return NULL;
256   }
257 }
258
259 gboolean
260 gst_rtp_g722_depay_plugin_init (GstPlugin * plugin)
261 {
262   return gst_element_register (plugin, "rtpg722depay",
263       GST_RANK_SECONDARY, GST_TYPE_RTP_G722_DEPAY);
264 }