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