gst/rtp/: parsers are depayers
[platform/upstream/gstreamer.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 "gstrtpL16depay.h"
20 #include "gstrtp-common.h"
21
22 /* elementfactory information */
23 static GstElementDetails gst_rtp_L16depay_details = {
24   "RTP packet parser",
25   "Codec/Depayr/Network",
26   "Extracts raw audio from RTP packets",
27   "Zeeshan Ali <zak147@yahoo.com>"
28 };
29
30 /* RtpL16Depay 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_rtp_L16depay_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_rtp_L16depay_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_rtp_L16depay_class_init (GstRtpL16DepayClass * klass);
64 static void gst_rtp_L16depay_base_init (GstRtpL16DepayClass * klass);
65 static void gst_rtp_L16depay_init (GstRtpL16Depay * rtpL16depay);
66
67 static void gst_rtp_L16depay_chain (GstPad * pad, GstData * _data);
68
69 static void gst_rtp_L16depay_set_property (GObject * object, guint prop_id,
70     const GValue * value, GParamSpec * pspec);
71 static void gst_rtp_L16depay_get_property (GObject * object, guint prop_id,
72     GValue * value, GParamSpec * pspec);
73 static GstStateChangeReturn gst_rtp_L16depay_change_state (GstElement *
74     element);
75
76 static GstElementClass *parent_class = NULL;
77
78 static GType
79 gst_rtp_L16depay_get_type (void)
80 {
81   static GType rtpL16depay_type = 0;
82
83   if (!rtpL16depay_type) {
84     static const GTypeInfo rtpL16depay_info = {
85       sizeof (GstRtpL16DepayClass),
86       (GBaseInitFunc) gst_rtp_L16depay_base_init,
87       NULL,
88       (GClassInitFunc) gst_rtp_L16depay_class_init,
89       NULL,
90       NULL,
91       sizeof (GstRtpL16Depay),
92       0,
93       (GInstanceInitFunc) gst_rtp_L16depay_init,
94     };
95
96     rtpL16depay_type =
97         g_type_register_static (GST_TYPE_ELEMENT, "GstRtpL16Depay",
98         &rtpL16depay_info, 0);
99   }
100   return rtpL16depay_type;
101 }
102
103 static void
104 gst_rtp_L16depay_base_init (GstRtpL16DepayClass * 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_rtp_L16depay_src_template));
110   gst_element_class_add_pad_template (element_class,
111       gst_static_pad_template_get (&gst_rtp_L16depay_sink_template));
112   gst_element_class_set_details (element_class, &gst_rtp_L16depay_details);
113 }
114
115 static void
116 gst_rtp_L16depay_class_init (GstRtpL16DepayClass * 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_rtp_L16depay_set_property;
134   gobject_class->get_property = gst_rtp_L16depay_get_property;
135
136   gstelement_class->change_state = gst_rtp_L16depay_change_state;
137 }
138
139 static void
140 gst_rtp_L16depay_init (GstRtpL16Depay * rtpL16depay)
141 {
142   rtpL16depay->srcpad =
143       gst_pad_new_from_template (gst_static_pad_template_get
144       (&gst_rtp_L16depay_src_template), "src");
145   rtpL16depay->sinkpad =
146       gst_pad_new_from_template (gst_static_pad_template_get
147       (&gst_rtp_L16depay_sink_template), "sink");
148   gst_element_add_pad (GST_ELEMENT (rtpL16depay), rtpL16depay->srcpad);
149   gst_element_add_pad (GST_ELEMENT (rtpL16depay), rtpL16depay->sinkpad);
150   gst_pad_set_chain_function (rtpL16depay->sinkpad, gst_rtp_L16depay_chain);
151
152   rtpL16depay->frequency = 44100;
153   rtpL16depay->channels = 2;
154
155   rtpL16depay->payload_type = PAYLOAD_L16_STEREO;
156 }
157
158 void
159 gst_rtp_L16depay_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_rtp_L16_caps_nego (GstRtpL16Depay * rtpL16depay)
174 {
175   GstCaps *caps;
176
177   caps =
178       gst_caps_copy (gst_static_caps_get (&gst_rtp_L16depay_src_template.
179           static_caps));
180
181   gst_caps_set_simple (caps,
182       "rate", G_TYPE_INT, rtpL16depay->frequency,
183       "channel", G_TYPE_INT, rtpL16depay->channels, NULL);
184
185   gst_pad_try_set_caps (rtpL16depay->srcpad, caps);
186 }
187
188 void
189 gst_rtp_L16depay_payloadtype_change (GstRtpL16Depay * rtpL16depay,
190     rtp_payload_t pt)
191 {
192   rtpL16depay->payload_type = pt;
193
194   switch (pt) {
195     case PAYLOAD_L16_MONO:
196       rtpL16depay->channels = 1;
197       break;
198     case PAYLOAD_L16_STEREO:
199       rtpL16depay->channels = 2;
200       break;
201     default:
202       g_warning ("unknown payload_t %d\n", pt);
203   }
204
205   gst_rtp_L16_caps_nego (rtpL16depay);
206 }
207
208 static void
209 gst_rtp_L16depay_chain (GstPad * pad, GstData * _data)
210 {
211   GstBuffer *buf = GST_BUFFER (_data);
212   GstRtpL16Depay *rtpL16depay;
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   rtpL16depay = GST_RTP_L16_DEPAY (GST_OBJECT_PARENT (pad));
222
223   g_return_if_fail (rtpL16depay != NULL);
224   g_return_if_fail (GST_IS_RTP_L16_DEPAY (rtpL16depay));
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 (rtpL16depay->srcpad) == NULL) {
235     gst_rtp_L16_caps_nego (rtpL16depay);
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 != rtpL16depay->payload_type) {
244     gst_rtp_L16depay_payloadtype_change (rtpL16depay, 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_rtp_L16depay_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_rtp_L16depay_ntohs (outbuf);
262 #endif
263
264   gst_pad_push (rtpL16depay->srcpad, GST_DATA (outbuf));
265
266   rtp_packet_free (packet);
267   gst_buffer_unref (buf);
268 }
269
270 static void
271 gst_rtp_L16depay_set_property (GObject * object, guint prop_id,
272     const GValue * value, GParamSpec * pspec)
273 {
274   GstRtpL16Depay *rtpL16depay;
275
276   g_return_if_fail (GST_IS_RTP_L16_DEPAY (object));
277   rtpL16depay = GST_RTP_L16_DEPAY (object);
278
279   switch (prop_id) {
280     case ARG_PAYLOAD_TYPE:
281       gst_rtp_L16depay_payloadtype_change (rtpL16depay,
282           g_value_get_int (value));
283       break;
284     case ARG_FREQUENCY:
285       rtpL16depay->frequency = g_value_get_int (value);
286       break;
287     default:
288       break;
289   }
290 }
291
292 static void
293 gst_rtp_L16depay_get_property (GObject * object, guint prop_id, GValue * value,
294     GParamSpec * pspec)
295 {
296   GstRtpL16Depay *rtpL16depay;
297
298   g_return_if_fail (GST_IS_RTP_L16_DEPAY (object));
299   rtpL16depay = GST_RTP_L16_DEPAY (object);
300
301   switch (prop_id) {
302     case ARG_PAYLOAD_TYPE:
303       g_value_set_int (value, rtpL16depay->payload_type);
304       break;
305     case ARG_FREQUENCY:
306       g_value_set_int (value, rtpL16depay->frequency);
307       break;
308     default:
309       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310       break;
311   }
312 }
313
314 static GstStateChangeReturn
315 gst_rtp_L16depay_change_state (GstElement * element, GstStateChange transition)
316 {
317   GstRtpL16Depay *rtpL16depay;
318
319   g_return_val_if_fail (GST_IS_RTP_L16_DEPAY (element),
320       GST_STATE_CHANGE_FAILURE);
321
322   rtpL16depay = GST_RTP_L16_DEPAY (element);
323
324   GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
325
326   switch (transition) {
327     case GST_STATE_CHANGE_NULL_TO_READY:
328       break;
329     case GST_STATE_CHANGE_READY_TO_NULL:
330       break;
331     default:
332       break;
333   }
334
335   /* if we haven't failed already, give the parent class a chance to ;-) */
336   if (GST_ELEMENT_CLASS (parent_class)->change_state)
337     return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
338
339   return GST_STATE_CHANGE_SUCCESS;
340 }
341
342 gboolean
343 gst_rtp_L16depay_plugin_init (GstPlugin * plugin)
344 {
345   return gst_element_register (plugin, "rtpL16depay",
346       GST_RANK_NONE, GST_TYPE_RTP_L16_DEPAY);
347 }