gst/rtp/: Small cleanups.
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpL16depay.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 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 #include <string.h>
24 #include "gstrtpL16depay.h"
25 #include "gstrtp-common.h"
26
27 /* elementfactory information */
28 static const GstElementDetails gst_rtp_L16depay_details =
29 GST_ELEMENT_DETAILS ("RTP packet parser",
30     "Codec/Depayr/Network",
31     "Extracts raw audio from RTP packets",
32     "Zeeshan Ali <zak147@yahoo.com>");
33
34 /* RtpL16Depay signals and args */
35 enum
36 {
37   /* FILL ME */
38   LAST_SIGNAL
39 };
40
41 enum
42 {
43   ARG_0,
44   ARG_FREQUENCY,
45   ARG_PAYLOAD_TYPE
46 };
47
48 static GstStaticPadTemplate gst_rtp_L16depay_src_template =
49 GST_STATIC_PAD_TEMPLATE ("src",
50     GST_PAD_SRC,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS ("audio/x-raw-int, "
53         "endianness = (int) BYTE_ORDER, "
54         "signed = (boolean) true, "
55         "width = (int) 16, "
56         "depth = (int) 16, "
57         "rate = (int) [ 1000, 48000 ], " "channels = (int) [ 1, 2 ]")
58     );
59
60 static GstStaticPadTemplate gst_rtp_L16depay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("application/x-rtp")
65     );
66
67 static void gst_rtp_L16depay_class_init (GstRtpL16DepayClass * klass);
68 static void gst_rtp_L16depay_base_init (GstRtpL16DepayClass * klass);
69 static void gst_rtp_L16depay_init (GstRtpL16Depay * rtpL16depay);
70
71 static void gst_rtp_L16depay_chain (GstPad * pad, GstData * _data);
72
73 static void gst_rtp_L16depay_set_property (GObject * object, guint prop_id,
74     const GValue * value, GParamSpec * pspec);
75 static void gst_rtp_L16depay_get_property (GObject * object, guint prop_id,
76     GValue * value, GParamSpec * pspec);
77 static GstStateChangeReturn gst_rtp_L16depay_change_state (GstElement *
78     element);
79
80 static GstElementClass *parent_class = NULL;
81
82 static GType
83 gst_rtp_L16depay_get_type (void)
84 {
85   static GType rtpL16depay_type = 0;
86
87   if (!rtpL16depay_type) {
88     static const GTypeInfo rtpL16depay_info = {
89       sizeof (GstRtpL16DepayClass),
90       (GBaseInitFunc) gst_rtp_L16depay_base_init,
91       NULL,
92       (GClassInitFunc) gst_rtp_L16depay_class_init,
93       NULL,
94       NULL,
95       sizeof (GstRtpL16Depay),
96       0,
97       (GInstanceInitFunc) gst_rtp_L16depay_init,
98     };
99
100     rtpL16depay_type =
101         g_type_register_static (GST_TYPE_ELEMENT, "GstRtpL16Depay",
102         &rtpL16depay_info, 0);
103   }
104   return rtpL16depay_type;
105 }
106
107 static void
108 gst_rtp_L16depay_base_init (GstRtpL16DepayClass * klass)
109 {
110   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
111
112   gst_element_class_add_pad_template (element_class,
113       gst_static_pad_template_get (&gst_rtp_L16depay_src_template));
114   gst_element_class_add_pad_template (element_class,
115       gst_static_pad_template_get (&gst_rtp_L16depay_sink_template));
116   gst_element_class_set_details (element_class, &gst_rtp_L16depay_details);
117 }
118
119 static void
120 gst_rtp_L16depay_class_init (GstRtpL16DepayClass * klass)
121 {
122   GObjectClass *gobject_class;
123   GstElementClass *gstelement_class;
124
125   gobject_class = (GObjectClass *) klass;
126   gstelement_class = (GstElementClass *) klass;
127
128   parent_class = g_type_class_peek_parent (klass);
129
130   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PAYLOAD_TYPE,
131       g_param_spec_int ("payload_type", "payload_type", "payload type",
132           G_MININT, G_MAXINT, PAYLOAD_L16_STEREO, G_PARAM_READABLE));
133   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREQUENCY,
134       g_param_spec_int ("frequency", "frequency", "frequency",
135           G_MININT, G_MAXINT, 44100, G_PARAM_READWRITE));
136
137   gobject_class->set_property = gst_rtp_L16depay_set_property;
138   gobject_class->get_property = gst_rtp_L16depay_get_property;
139
140   gstelement_class->change_state = gst_rtp_L16depay_change_state;
141 }
142
143 static void
144 gst_rtp_L16depay_init (GstRtpL16Depay * rtpL16depay)
145 {
146   rtpL16depay->srcpad =
147       gst_pad_new_from_static_template (&gst_rtp_L16depay_src_template, "src");
148   rtpL16depay->sinkpad =
149       gst_pad_new_from_static_template (&gst_rtp_L16depay_sink_template,
150       "sink");
151   gst_element_add_pad (GST_ELEMENT (rtpL16depay), rtpL16depay->srcpad);
152   gst_element_add_pad (GST_ELEMENT (rtpL16depay), rtpL16depay->sinkpad);
153   gst_pad_set_chain_function (rtpL16depay->sinkpad, gst_rtp_L16depay_chain);
154
155   rtpL16depay->frequency = 44100;
156   rtpL16depay->channels = 2;
157
158   rtpL16depay->payload_type = PAYLOAD_L16_STEREO;
159 }
160
161 void
162 gst_rtp_L16depay_ntohs (GstBuffer * buf)
163 {
164   gint16 *i, *len;
165
166   /* FIXME: is this code correct or even sane at all? */
167   i = (gint16 *) GST_BUFFER_DATA (buf);
168   len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
169
170   for (; i < len; i++) {
171     *i = g_ntohs (*i);
172   }
173 }
174
175 void
176 gst_rtp_L16_caps_nego (GstRtpL16Depay * rtpL16depay)
177 {
178   GstCaps *caps;
179
180   caps =
181       gst_caps_copy (gst_static_caps_get (&gst_rtp_L16depay_src_template.
182           static_caps));
183
184   gst_caps_set_simple (caps,
185       "rate", G_TYPE_INT, rtpL16depay->frequency,
186       "channel", G_TYPE_INT, rtpL16depay->channels, NULL);
187
188   gst_pad_try_set_caps (rtpL16depay->srcpad, caps);
189 }
190
191 void
192 gst_rtp_L16depay_payloadtype_change (GstRtpL16Depay * rtpL16depay,
193     rtp_payload_t pt)
194 {
195   rtpL16depay->payload_type = pt;
196
197   switch (pt) {
198     case PAYLOAD_L16_MONO:
199       rtpL16depay->channels = 1;
200       break;
201     case PAYLOAD_L16_STEREO:
202       rtpL16depay->channels = 2;
203       break;
204     default:
205       g_warning ("unknown payload_t %d\n", pt);
206   }
207
208   gst_rtp_L16_caps_nego (rtpL16depay);
209 }
210
211 static void
212 gst_rtp_L16depay_chain (GstPad * pad, GstData * _data)
213 {
214   GstBuffer *buf = GST_BUFFER (_data);
215   GstRtpL16Depay *rtpL16depay;
216   GstBuffer *outbuf;
217   Rtp_Packet packet;
218   rtp_payload_t pt;
219
220   g_return_if_fail (pad != NULL);
221   g_return_if_fail (GST_IS_PAD (pad));
222   g_return_if_fail (buf != NULL);
223
224   rtpL16depay = GST_RTP_L16_DEPAY (GST_OBJECT_PARENT (pad));
225
226   g_return_if_fail (rtpL16depay != NULL);
227   g_return_if_fail (GST_IS_RTP_L16_DEPAY (rtpL16depay));
228
229   if (GST_IS_EVENT (buf)) {
230     GstEvent *event = GST_EVENT (buf);
231
232     gst_pad_event_default (pad, event);
233
234     return;
235   }
236
237   if (GST_PAD_CAPS (rtpL16depay->srcpad) == NULL) {
238     gst_rtp_L16_caps_nego (rtpL16depay);
239   }
240
241   packet =
242       rtp_packet_new_copy_data (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
243
244   pt = rtp_packet_get_payload_type (packet);
245
246   if (pt != rtpL16depay->payload_type) {
247     gst_rtp_L16depay_payloadtype_change (rtpL16depay, pt);
248   }
249
250   outbuf = gst_buffer_new ();
251   GST_BUFFER_SIZE (outbuf) = rtp_packet_get_payload_len (packet);
252   GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
253   GST_BUFFER_TIMESTAMP (outbuf) =
254       g_ntohl (rtp_packet_get_timestamp (packet)) * GST_SECOND;
255
256   memcpy (GST_BUFFER_DATA (outbuf), rtp_packet_get_payload (packet),
257       GST_BUFFER_SIZE (outbuf));
258
259   GST_DEBUG ("gst_rtp_L16depay_chain: pushing buffer of size %d",
260       GST_BUFFER_SIZE (outbuf));
261
262   /* FIXME: According to RFC 1890, this is required, right? */
263 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
264   gst_rtp_L16depay_ntohs (outbuf);
265 #endif
266
267   gst_pad_push (rtpL16depay->srcpad, GST_DATA (outbuf));
268
269   rtp_packet_free (packet);
270   gst_buffer_unref (buf);
271 }
272
273 static void
274 gst_rtp_L16depay_set_property (GObject * object, guint prop_id,
275     const GValue * value, GParamSpec * pspec)
276 {
277   GstRtpL16Depay *rtpL16depay;
278
279   g_return_if_fail (GST_IS_RTP_L16_DEPAY (object));
280   rtpL16depay = GST_RTP_L16_DEPAY (object);
281
282   switch (prop_id) {
283     case ARG_PAYLOAD_TYPE:
284       gst_rtp_L16depay_payloadtype_change (rtpL16depay,
285           g_value_get_int (value));
286       break;
287     case ARG_FREQUENCY:
288       rtpL16depay->frequency = g_value_get_int (value);
289       break;
290     default:
291       break;
292   }
293 }
294
295 static void
296 gst_rtp_L16depay_get_property (GObject * object, guint prop_id, GValue * value,
297     GParamSpec * pspec)
298 {
299   GstRtpL16Depay *rtpL16depay;
300
301   g_return_if_fail (GST_IS_RTP_L16_DEPAY (object));
302   rtpL16depay = GST_RTP_L16_DEPAY (object);
303
304   switch (prop_id) {
305     case ARG_PAYLOAD_TYPE:
306       g_value_set_int (value, rtpL16depay->payload_type);
307       break;
308     case ARG_FREQUENCY:
309       g_value_set_int (value, rtpL16depay->frequency);
310       break;
311     default:
312       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313       break;
314   }
315 }
316
317 static GstStateChangeReturn
318 gst_rtp_L16depay_change_state (GstElement * element, GstStateChange transition)
319 {
320   GstRtpL16Depay *rtpL16depay;
321   GstStateChangeReturn ret;
322
323   rtpL16depay = GST_RTP_L16_DEPAY (element);
324
325   GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
326
327
328   switch (transition) {
329     case GST_STATE_CHANGE_NULL_TO_READY:
330       break;
331     default:
332       break;
333   }
334   /* if we haven't failed already, give the parent class a chance to ;-) */
335   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
336
337   switch (transition) {
338     case GST_STATE_CHANGE_READY_TO_NULL:
339       break;
340     default:
341       break;
342   }
343
344   return ret;
345 }
346
347 gboolean
348 gst_rtp_L16depay_plugin_init (GstPlugin * plugin)
349 {
350   return gst_element_register (plugin, "rtpL16depay",
351       GST_RANK_NONE, GST_TYPE_RTP_L16_DEPAY);
352 }