rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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         "clock-rate = (int) 8000, "
52         "encoding-name = (string) \"ILBC\", "
53         "mode = (string) { \"20\", \"30\" }")
54     );
55
56 static GstStaticPadTemplate gst_rtp_ilbc_depay_src_template =
57 GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("audio/x-iLBC, " "mode = (int) { 20, 30 }")
61     );
62
63 static void gst_ilbc_depay_set_property (GObject * object,
64     guint prop_id, const GValue * value, GParamSpec * pspec);
65 static void gst_ilbc_depay_get_property (GObject * object,
66     guint prop_id, GValue * value, GParamSpec * pspec);
67
68 static GstBuffer *gst_rtp_ilbc_depay_process (GstRTPBaseDepayload * depayload,
69     GstBuffer * buf);
70 static gboolean gst_rtp_ilbc_depay_setcaps (GstRTPBaseDepayload * depayload,
71     GstCaps * caps);
72
73 #define gst_rtp_ilbc_depay_parent_class parent_class
74 G_DEFINE_TYPE (GstRTPiLBCDepay, gst_rtp_ilbc_depay,
75     GST_TYPE_RTP_BASE_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_class_init (GstRTPiLBCDepayClass * klass)
96 {
97   GObjectClass *gobject_class;
98   GstElementClass *gstelement_class;
99   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
100
101   gobject_class = (GObjectClass *) klass;
102   gstelement_class = (GstElementClass *) klass;
103   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
104
105   gobject_class->set_property = gst_ilbc_depay_set_property;
106   gobject_class->get_property = gst_ilbc_depay_get_property;
107
108   /* FIXME, mode is in the caps */
109   g_object_class_install_property (gobject_class, PROP_MODE,
110       g_param_spec_enum ("mode", "Mode", "iLBC frame mode",
111           GST_TYPE_ILBC_MODE, DEFAULT_MODE,
112           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
113
114   gst_element_class_add_pad_template (gstelement_class,
115       gst_static_pad_template_get (&gst_rtp_ilbc_depay_src_template));
116   gst_element_class_add_pad_template (gstelement_class,
117       gst_static_pad_template_get (&gst_rtp_ilbc_depay_sink_template));
118
119   gst_element_class_set_static_metadata (gstelement_class,
120       "RTP iLBC depayloader", "Codec/Depayloader/Network/RTP",
121       "Extracts iLBC audio from RTP packets (RFC 3952)",
122       "Philippe Kalaf <philippe.kalaf@collabora.co.uk>");
123
124   gstrtpbasedepayload_class->process = gst_rtp_ilbc_depay_process;
125   gstrtpbasedepayload_class->set_caps = gst_rtp_ilbc_depay_setcaps;
126 }
127
128 static void
129 gst_rtp_ilbc_depay_init (GstRTPiLBCDepay * rtpilbcdepay)
130 {
131   /* Set default mode */
132   rtpilbcdepay->mode = DEFAULT_MODE;
133 }
134
135 static gboolean
136 gst_rtp_ilbc_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
137 {
138   GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (depayload);
139   GstCaps *srccaps;
140   GstStructure *structure;
141   const gchar *mode_str = NULL;
142   gint mode, clock_rate;
143   gboolean ret;
144
145   structure = gst_caps_get_structure (caps, 0);
146
147   mode = rtpilbcdepay->mode;
148
149   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
150     clock_rate = 8000;
151   depayload->clock_rate = clock_rate;
152
153   /* parse mode, if we can */
154   mode_str = gst_structure_get_string (structure, "mode");
155   if (mode_str) {
156     mode = strtol (mode_str, NULL, 10);
157     if (mode != 20 && mode != 30)
158       mode = rtpilbcdepay->mode;
159   }
160
161   rtpilbcdepay->mode = mode;
162
163   srccaps = gst_caps_new_simple ("audio/x-iLBC",
164       "mode", G_TYPE_INT, rtpilbcdepay->mode, NULL);
165   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
166
167   GST_DEBUG ("set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
168   gst_caps_unref (srccaps);
169
170   return ret;
171 }
172
173 static GstBuffer *
174 gst_rtp_ilbc_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
175 {
176   GstBuffer *outbuf;
177   gboolean marker;
178   GstRTPBuffer rtp = { NULL };
179
180   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
181
182   marker = gst_rtp_buffer_get_marker (&rtp);
183
184   GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
185       gst_buffer_get_size (buf), marker,
186       gst_rtp_buffer_get_timestamp (&rtp), gst_rtp_buffer_get_seq (&rtp));
187
188   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
189
190   gst_rtp_buffer_unmap (&rtp);
191
192   if (marker && outbuf) {
193     /* mark start of talkspurt with RESYNC */
194     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
195   }
196
197   return outbuf;
198 }
199
200 static void
201 gst_ilbc_depay_set_property (GObject * object,
202     guint prop_id, const GValue * value, GParamSpec * pspec)
203 {
204   GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (object);
205
206   switch (prop_id) {
207     case PROP_MODE:
208       rtpilbcdepay->mode = g_value_get_enum (value);
209       break;
210     default:
211       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212       break;
213   }
214 }
215
216 static void
217 gst_ilbc_depay_get_property (GObject * object,
218     guint prop_id, GValue * value, GParamSpec * pspec)
219 {
220   GstRTPiLBCDepay *rtpilbcdepay = GST_RTP_ILBC_DEPAY (object);
221
222   switch (prop_id) {
223     case PROP_MODE:
224       g_value_set_enum (value, rtpilbcdepay->mode);
225       break;
226     default:
227       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228       break;
229   }
230 }
231
232 gboolean
233 gst_rtp_ilbc_depay_plugin_init (GstPlugin * plugin)
234 {
235   return gst_element_register (plugin, "rtpilbcdepay",
236       GST_RANK_SECONDARY, GST_TYPE_RTP_ILBC_DEPAY);
237 }