Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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 (GstBaseRTPDepayload * depayload,
70     GstBuffer * buf);
71 static gboolean gst_rtp_ilbc_depay_setcaps (GstBaseRTPDepayload * depayload,
72     GstCaps * caps);
73
74 GST_BOILERPLATE (GstRTPiLBCDepay, gst_rtp_ilbc_depay, GstBaseRTPDepayload,
75     GST_TYPE_BASE_RTP_DEPAYLOAD);
76
77 #define GST_TYPE_ILBC_MODE (gst_ilbc_mode_get_type())
78 static GType
79 gst_ilbc_mode_get_type (void)
80 {
81   static GType ilbc_mode_type = 0;
82   static const GEnumValue ilbc_modes[] = {
83     {GST_ILBC_MODE_20, "20ms frames", "20ms"},
84     {GST_ILBC_MODE_30, "30ms frames", "30ms"},
85     {0, NULL, NULL},
86   };
87
88   if (!ilbc_mode_type) {
89     ilbc_mode_type = g_enum_register_static ("iLBCMode", ilbc_modes);
90   }
91   return ilbc_mode_type;
92 }
93
94 static void
95 gst_rtp_ilbc_depay_base_init (gpointer klass)
96 {
97   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
98
99   gst_element_class_add_static_pad_template (element_class,
100       &gst_rtp_ilbc_depay_src_template);
101   gst_element_class_add_static_pad_template (element_class,
102       &gst_rtp_ilbc_depay_sink_template);
103   gst_element_class_set_details_simple (element_class, "RTP iLBC depayloader",
104       "Codec/Depayloader/Network/RTP",
105       "Extracts iLBC audio from RTP packets (RFC 3952)",
106       "Philippe Kalaf <philippe.kalaf@collabora.co.uk>");
107 }
108
109 static void
110 gst_rtp_ilbc_depay_class_init (GstRTPiLBCDepayClass * klass)
111 {
112   GObjectClass *gobject_class;
113   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
114
115   gobject_class = (GObjectClass *) klass;
116   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
117
118   gobject_class->set_property = gst_ilbc_depay_set_property;
119   gobject_class->get_property = gst_ilbc_depay_get_property;
120
121   /* FIXME, mode is in the caps */
122   g_object_class_install_property (gobject_class, PROP_MODE,
123       g_param_spec_enum ("mode", "Mode", "iLBC frame mode",
124           GST_TYPE_ILBC_MODE, DEFAULT_MODE,
125           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126
127   gstbasertpdepayload_class->process = gst_rtp_ilbc_depay_process;
128   gstbasertpdepayload_class->set_caps = gst_rtp_ilbc_depay_setcaps;
129 }
130
131 static void
132 gst_rtp_ilbc_depay_init (GstRTPiLBCDepay * rtpilbcdepay,
133     GstRTPiLBCDepayClass * klass)
134 {
135   /* Set default mode */
136   rtpilbcdepay->mode = DEFAULT_MODE;
137 }
138
139 static gboolean
140 gst_rtp_ilbc_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
141 {
142   GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (depayload);
143   GstCaps *srccaps;
144   GstStructure *structure;
145   const gchar *mode_str = NULL;
146   gint mode, clock_rate;
147   gboolean ret;
148
149   structure = gst_caps_get_structure (caps, 0);
150
151   mode = rtpilbcdepay->mode;
152
153   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
154     clock_rate = 8000;
155   depayload->clock_rate = clock_rate;
156
157   /* parse mode, if we can */
158   mode_str = gst_structure_get_string (structure, "mode");
159   if (mode_str) {
160     mode = strtol (mode_str, NULL, 10);
161     if (mode != 20 && mode != 30)
162       mode = rtpilbcdepay->mode;
163   }
164
165   rtpilbcdepay->mode = mode;
166
167   srccaps = gst_caps_new_simple ("audio/x-iLBC",
168       "mode", G_TYPE_INT, rtpilbcdepay->mode, NULL);
169   ret = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
170
171   GST_DEBUG ("set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
172   gst_caps_unref (srccaps);
173
174   return ret;
175 }
176
177 static GstBuffer *
178 gst_rtp_ilbc_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
179 {
180   GstBuffer *outbuf;
181   gboolean marker;
182
183   marker = gst_rtp_buffer_get_marker (buf);
184
185   GST_DEBUG ("process : got %d bytes, mark %d ts %u seqn %d",
186       GST_BUFFER_SIZE (buf), marker,
187       gst_rtp_buffer_get_timestamp (buf), gst_rtp_buffer_get_seq (buf));
188
189   outbuf = gst_rtp_buffer_get_payload_buffer (buf);
190
191   if (marker && outbuf) {
192     /* mark start of talkspurt with DISCONT */
193     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
194   }
195
196   return outbuf;
197 }
198
199 static void
200 gst_ilbc_depay_set_property (GObject * object,
201     guint prop_id, const GValue * value, GParamSpec * pspec)
202 {
203   GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (object);
204
205   switch (prop_id) {
206     case PROP_MODE:
207       rtpilbcdepay->mode = g_value_get_enum (value);
208       break;
209     default:
210       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211       break;
212   }
213 }
214
215 static void
216 gst_ilbc_depay_get_property (GObject * object,
217     guint prop_id, GValue * value, GParamSpec * pspec)
218 {
219   GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (object);
220
221   switch (prop_id) {
222     case PROP_MODE:
223       g_value_set_enum (value, rtpilbcdepay->mode);
224       break;
225     default:
226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227       break;
228   }
229 }
230
231 gboolean
232 gst_rtp_ilbc_depay_plugin_init (GstPlugin * plugin)
233 {
234   return gst_element_register (plugin, "rtpilbcdepay",
235       GST_RANK_SECONDARY, GST_TYPE_RTP_ILBC_DEPAY);
236 }