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