gstdepay: check for correct fragment offset
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpilbcdepay.c
1 /* GStreamer
2  * Copyright (C) <2006> Philippe Khalaf <burger@speedy.org>
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 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpilbcdepay.h"
28
29 /* RtpiLBCDepay signals and args */
30 enum
31 {
32   /* FILL ME */
33   LAST_SIGNAL
34 };
35
36 #define DEFAULT_MODE GST_ILBC_MODE_30
37
38 enum
39 {
40   PROP_0,
41   PROP_MODE
42 };
43
44 /* FIXME, mode should be string because it is a parameter in SDP fmtp */
45 static GstStaticPadTemplate gst_rtp_ilbc_depay_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
47     GST_PAD_SINK,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("application/x-rtp, "
50         "media = (string) \"audio\", "
51         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52         "clock-rate = (int) 8000, "
53         "encoding-name = (string) \"ILBC\", "
54         "mode = (string) { \"20\", \"30\" }")
55     );
56
57 static GstStaticPadTemplate gst_rtp_ilbc_depay_src_template =
58 GST_STATIC_PAD_TEMPLATE ("src",
59     GST_PAD_SRC,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("audio/x-iLBC, " "mode = (int) { 20, 30 }")
62     );
63
64 static void gst_ilbc_depay_set_property (GObject * object,
65     guint prop_id, const GValue * value, GParamSpec * pspec);
66 static void gst_ilbc_depay_get_property (GObject * object,
67     guint prop_id, GValue * value, GParamSpec * pspec);
68
69 static GstBuffer *gst_rtp_ilbc_depay_process (GstRTPBaseDepayload * depayload,
70     GstBuffer * buf);
71 static gboolean gst_rtp_ilbc_depay_setcaps (GstRTPBaseDepayload * depayload,
72     GstCaps * caps);
73
74 #define gst_rtp_ilbc_depay_parent_class parent_class
75 G_DEFINE_TYPE (GstRTPiLBCDepay, gst_rtp_ilbc_depay,
76     GST_TYPE_RTP_BASE_DEPAYLOAD);
77
78 #define GST_TYPE_ILBC_MODE (gst_ilbc_mode_get_type())
79 static GType
80 gst_ilbc_mode_get_type (void)
81 {
82   static GType ilbc_mode_type = 0;
83   static const GEnumValue ilbc_modes[] = {
84     {GST_ILBC_MODE_20, "20ms frames", "20ms"},
85     {GST_ILBC_MODE_30, "30ms frames", "30ms"},
86     {0, NULL, NULL},
87   };
88
89   if (!ilbc_mode_type) {
90     ilbc_mode_type = g_enum_register_static ("iLBCMode", ilbc_modes);
91   }
92   return ilbc_mode_type;
93 }
94
95 static void
96 gst_rtp_ilbc_depay_class_init (GstRTPiLBCDepayClass * klass)
97 {
98   GObjectClass *gobject_class;
99   GstElementClass *gstelement_class;
100   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
101
102   gobject_class = (GObjectClass *) klass;
103   gstelement_class = (GstElementClass *) klass;
104   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
105
106   gobject_class->set_property = gst_ilbc_depay_set_property;
107   gobject_class->get_property = gst_ilbc_depay_get_property;
108
109   /* FIXME, mode is in the caps */
110   g_object_class_install_property (gobject_class, PROP_MODE,
111       g_param_spec_enum ("mode", "Mode", "iLBC frame mode",
112           GST_TYPE_ILBC_MODE, DEFAULT_MODE,
113           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
114
115   gst_element_class_add_pad_template (gstelement_class,
116       gst_static_pad_template_get (&gst_rtp_ilbc_depay_src_template));
117   gst_element_class_add_pad_template (gstelement_class,
118       gst_static_pad_template_get (&gst_rtp_ilbc_depay_sink_template));
119
120   gst_element_class_set_static_metadata (gstelement_class,
121       "RTP iLBC depayloader", "Codec/Depayloader/Network/RTP",
122       "Extracts iLBC audio from RTP packets (RFC 3952)",
123       "Philippe Kalaf <philippe.kalaf@collabora.co.uk>");
124
125   gstrtpbasedepayload_class->process = gst_rtp_ilbc_depay_process;
126   gstrtpbasedepayload_class->set_caps = gst_rtp_ilbc_depay_setcaps;
127 }
128
129 static void
130 gst_rtp_ilbc_depay_init (GstRTPiLBCDepay * rtpilbcdepay)
131 {
132   /* Set default mode */
133   rtpilbcdepay->mode = DEFAULT_MODE;
134 }
135
136 static gboolean
137 gst_rtp_ilbc_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
138 {
139   GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (depayload);
140   GstCaps *srccaps;
141   GstStructure *structure;
142   const gchar *mode_str = NULL;
143   gint mode, clock_rate;
144   gboolean ret;
145
146   structure = gst_caps_get_structure (caps, 0);
147
148   mode = rtpilbcdepay->mode;
149
150   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
151     clock_rate = 8000;
152   depayload->clock_rate = clock_rate;
153
154   /* parse mode, if we can */
155   mode_str = gst_structure_get_string (structure, "mode");
156   if (mode_str) {
157     mode = strtol (mode_str, NULL, 10);
158     if (mode != 20 && mode != 30)
159       mode = rtpilbcdepay->mode;
160   }
161
162   rtpilbcdepay->mode = mode;
163
164   srccaps = gst_caps_new_simple ("audio/x-iLBC",
165       "mode", G_TYPE_INT, rtpilbcdepay->mode, NULL);
166   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
167
168   GST_DEBUG ("set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
169   gst_caps_unref (srccaps);
170
171   return ret;
172 }
173
174 static GstBuffer *
175 gst_rtp_ilbc_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
176 {
177   GstBuffer *outbuf;
178   gboolean marker;
179   GstRTPBuffer rtp = { NULL };
180
181   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
182
183   marker = gst_rtp_buffer_get_marker (&rtp);
184
185   GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
186       gst_buffer_get_size (buf), marker,
187       gst_rtp_buffer_get_timestamp (&rtp), gst_rtp_buffer_get_seq (&rtp));
188
189   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
190
191   gst_rtp_buffer_unmap (&rtp);
192
193   if (marker && outbuf) {
194     /* mark start of talkspurt with DISCONT */
195     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
196   }
197
198   return outbuf;
199 }
200
201 static void
202 gst_ilbc_depay_set_property (GObject * object,
203     guint prop_id, const GValue * value, GParamSpec * pspec)
204 {
205   GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (object);
206
207   switch (prop_id) {
208     case PROP_MODE:
209       rtpilbcdepay->mode = g_value_get_enum (value);
210       break;
211     default:
212       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213       break;
214   }
215 }
216
217 static void
218 gst_ilbc_depay_get_property (GObject * object,
219     guint prop_id, GValue * value, GParamSpec * pspec)
220 {
221   GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (object);
222
223   switch (prop_id) {
224     case PROP_MODE:
225       g_value_set_enum (value, rtpilbcdepay->mode);
226       break;
227     default:
228       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229       break;
230   }
231 }
232
233 gboolean
234 gst_rtp_ilbc_depay_plugin_init (GstPlugin * plugin)
235 {
236   return gst_element_register (plugin, "rtpilbcdepay",
237       GST_RANK_SECONDARY, GST_TYPE_RTP_ILBC_DEPAY);
238 }