8f1b301ab6848bd85fc3aae6aed211081f7a93e2
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpgsmparse.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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 
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #  include "config.h"
17 #endif
18
19 #include <string.h>
20 #include "gstrtpgsmparse.h"
21 #include "gstrtp-common.h"
22
23 /* elementfactory information */
24 static GstElementDetails gst_rtp_gsmparse_details = {
25   "RTP packet parser",
26   "Codec/Parser/Network",
27   "Extracts GSM audio from RTP packets",
28   "Zeeshan Ali <zak147@yahoo.com>"
29 };
30
31 /* RtpGSMParse signals and args */
32 enum
33 {
34   /* FILL ME */
35   LAST_SIGNAL
36 };
37
38 enum
39 {
40   ARG_0,
41   ARG_FREQUENCY
42 };
43
44 static GstStaticPadTemplate gst_rtpgsmparse_src_template =
45 GST_STATIC_PAD_TEMPLATE (
46     "src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS, 
49     GST_STATIC_CAPS ( "audio/x-gsm, "
50       "rate = (int) [ 1000, 48000 ]"
51     )
52 );
53
54 static GstStaticPadTemplate gst_rtpgsmparse_sink_template =
55 GST_STATIC_PAD_TEMPLATE (
56     "sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS, 
59     GST_STATIC_CAPS ("application/x-rtp")
60 );
61
62
63 static void gst_rtpgsmparse_class_init (GstRtpGSMParseClass * klass);
64 static void gst_rtpgsmparse_base_init (GstRtpGSMParseClass * klass);
65 static void gst_rtpgsmparse_init (GstRtpGSMParse * rtpgsmparse);
66
67 static void gst_rtpgsmparse_chain (GstPad * pad, GstData *_data);
68
69 static void gst_rtpgsmparse_set_property (GObject * object, guint prop_id,
70                                    const GValue * value, GParamSpec * pspec);
71 static void gst_rtpgsmparse_get_property (GObject * object, guint prop_id,
72                                    GValue * value, GParamSpec * pspec);
73 static GstElementStateReturn gst_rtpgsmparse_change_state (GstElement * element);
74
75 static GstElementClass *parent_class = NULL;
76
77 static GType gst_rtpgsmparse_get_type (void)
78 {
79   static GType rtpgsmparse_type = 0;
80
81   if (!rtpgsmparse_type) {
82     static const GTypeInfo rtpgsmparse_info = {
83       sizeof (GstRtpGSMParseClass),
84       (GBaseInitFunc) gst_rtpgsmparse_base_init,
85       NULL,
86       (GClassInitFunc) gst_rtpgsmparse_class_init,
87       NULL,
88       NULL,
89       sizeof (GstRtpGSMParse),
90       0,
91       (GInstanceInitFunc) gst_rtpgsmparse_init,
92     };
93
94     rtpgsmparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstRtpGSMParse", &rtpgsmparse_info, 0);
95   }
96   return rtpgsmparse_type;
97 }
98
99 static void
100 gst_rtpgsmparse_base_init (GstRtpGSMParseClass * klass)
101 {
102   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
103
104   gst_element_class_add_pad_template (element_class,
105       gst_static_pad_template_get(&gst_rtpgsmparse_src_template));
106   gst_element_class_add_pad_template (element_class,
107       gst_static_pad_template_get(&gst_rtpgsmparse_sink_template));
108   gst_element_class_set_details (element_class, &gst_rtp_gsmparse_details);
109 }
110
111 static void
112 gst_rtpgsmparse_class_init (GstRtpGSMParseClass * klass)
113 {
114   GObjectClass *gobject_class;
115   GstElementClass *gstelement_class;
116
117   gobject_class = (GObjectClass *) klass;
118   gstelement_class = (GstElementClass *) klass;
119
120   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
121
122   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREQUENCY, 
123                 g_param_spec_int ("frequency", "frequency", "frequency", 
124                         G_MININT, G_MAXINT, 8000, G_PARAM_READWRITE));
125
126   gobject_class->set_property = gst_rtpgsmparse_set_property;
127   gobject_class->get_property = gst_rtpgsmparse_get_property;
128
129   gstelement_class->change_state = gst_rtpgsmparse_change_state;
130 }
131
132 static void
133 gst_rtpgsmparse_init (GstRtpGSMParse * rtpgsmparse)
134 {
135   rtpgsmparse->srcpad = gst_pad_new_from_template (
136       gst_static_pad_template_get(&gst_rtpgsmparse_src_template), "src");
137   rtpgsmparse->sinkpad = gst_pad_new_from_template (
138       gst_static_pad_template_get(&gst_rtpgsmparse_sink_template), "sink");
139   gst_element_add_pad (GST_ELEMENT (rtpgsmparse), rtpgsmparse->srcpad);
140   gst_element_add_pad (GST_ELEMENT (rtpgsmparse), rtpgsmparse->sinkpad);
141   gst_pad_set_chain_function (rtpgsmparse->sinkpad, gst_rtpgsmparse_chain);
142
143   rtpgsmparse->frequency = 8000;
144 }
145
146 void
147 gst_rtpgsmparse_ntohs (GstBuffer *buf)
148 {
149   gint16 *i, *len;
150
151   /* FIXME: is this code correct or even sane at all? */
152   i = (gint16 *) GST_BUFFER_DATA(buf); 
153   len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
154
155   for (; i<len; i++) {
156       *i = g_ntohs (*i);
157   }
158 }
159
160 void
161 gst_rtpgsm_caps_nego (GstRtpGSMParse *rtpgsmparse)
162 {
163   GstCaps *caps;
164
165   caps = gst_caps_new_simple ("audio/x-gsm",
166        "rate", G_TYPE_INT, rtpgsmparse->frequency);
167
168   gst_pad_try_set_caps (rtpgsmparse->srcpad, caps);
169 }
170
171 static void
172 gst_rtpgsmparse_chain (GstPad * pad, GstData *_data)
173 {
174   GstBuffer *buf = GST_BUFFER (_data);
175   GstRtpGSMParse *rtpgsmparse;
176   GstBuffer *outbuf;
177   Rtp_Packet packet;
178   rtp_payload_t pt;
179
180   g_return_if_fail (pad != NULL);
181   g_return_if_fail (GST_IS_PAD (pad));
182   g_return_if_fail (buf != NULL);
183
184   rtpgsmparse = GST_RTP_GSM_PARSE (GST_OBJECT_PARENT (pad));
185
186   g_return_if_fail (rtpgsmparse != NULL);
187   g_return_if_fail (GST_IS_RTP_GSM_PARSE (rtpgsmparse));
188
189   if (GST_IS_EVENT (buf)) {
190     GstEvent *event = GST_EVENT (buf);
191     gst_pad_event_default (pad, event);
192   
193     return;
194   }
195
196   if (GST_PAD_CAPS (rtpgsmparse->srcpad) == NULL) {
197     gst_rtpgsm_caps_nego (rtpgsmparse);
198   }
199
200   packet = rtp_packet_new_copy_data (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
201
202   pt = rtp_packet_get_payload_type (packet);
203
204   if (pt != PAYLOAD_GSM) {
205     g_warning ("Unexpected paload type %u\n", pt);
206     rtp_packet_free (packet);
207     gst_buffer_unref (buf);
208     return;
209   }
210
211   outbuf = gst_buffer_new ();
212   GST_BUFFER_SIZE (outbuf) = rtp_packet_get_payload_len (packet);
213   GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
214   GST_BUFFER_TIMESTAMP (outbuf) = g_ntohl (rtp_packet_get_timestamp (packet)) * GST_SECOND;
215
216   memcpy (GST_BUFFER_DATA (outbuf), rtp_packet_get_payload (packet), GST_BUFFER_SIZE (outbuf));
217         
218   GST_DEBUG ("gst_rtpgsmparse_chain: pushing buffer of size %d", GST_BUFFER_SIZE(outbuf));
219
220 /* FIXME: According to RFC 1890, this is required, right? */
221 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
222      gst_rtpgsmparse_ntohs (outbuf);
223 #endif
224
225   gst_pad_push (rtpgsmparse->srcpad, GST_DATA (outbuf));
226
227   rtp_packet_free (packet);
228   gst_buffer_unref (buf);
229 }
230
231 static void
232 gst_rtpgsmparse_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
233 {
234   GstRtpGSMParse *rtpgsmparse;
235
236   /* it's not null if we got it, but it might not be ours */
237   g_return_if_fail (GST_IS_RTP_GSM_PARSE (object));
238   rtpgsmparse = GST_RTP_GSM_PARSE (object);
239
240   switch (prop_id) {
241     case ARG_FREQUENCY:
242       rtpgsmparse->frequency = g_value_get_int (value);
243       break;
244     default:
245       break;
246   }
247 }
248
249 static void
250 gst_rtpgsmparse_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
251 {
252   GstRtpGSMParse *rtpgsmparse;
253
254   /* it's not null if we got it, but it might not be ours */
255   g_return_if_fail (GST_IS_RTP_GSM_PARSE (object));
256   rtpgsmparse = GST_RTP_GSM_PARSE (object);
257
258   switch (prop_id) {
259     case ARG_FREQUENCY:
260       g_value_set_int (value, rtpgsmparse->frequency);
261       break;
262     default:
263       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264       break;
265   }
266 }
267
268 static GstElementStateReturn
269 gst_rtpgsmparse_change_state (GstElement * element)
270 {
271   GstRtpGSMParse *rtpgsmparse;
272
273   g_return_val_if_fail (GST_IS_RTP_GSM_PARSE (element), GST_STATE_FAILURE);
274
275   rtpgsmparse = GST_RTP_GSM_PARSE (element);
276
277   GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
278
279   switch (GST_STATE_TRANSITION (element)) {
280     case GST_STATE_NULL_TO_READY:
281       break;
282     case GST_STATE_READY_TO_NULL:
283       break;
284     default:
285       break;
286   }
287
288   /* if we haven't failed already, give the parent class a chance to ;-) */
289   if (GST_ELEMENT_CLASS (parent_class)->change_state)
290     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
291
292   return GST_STATE_SUCCESS;
293 }
294
295 gboolean
296 gst_rtpgsmparse_plugin_init (GstPlugin * plugin)
297 {
298   return gst_element_register (plugin, "rtpgsmparse",
299                                GST_RANK_NONE, GST_TYPE_RTP_GSM_PARSE);
300 }