2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
24 #include "gstrtpL16depay.h"
25 #include "gstrtp-common.h"
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>");
34 /* RtpL16Depay signals and args */
48 static GstStaticPadTemplate gst_rtp_L16depay_src_template =
49 GST_STATIC_PAD_TEMPLATE ("src",
52 GST_STATIC_CAPS ("audio/x-raw-int, "
53 "endianness = (int) BYTE_ORDER, "
54 "signed = (boolean) true, "
57 "rate = (int) [ 1000, 48000 ], " "channels = (int) [ 1, 2 ]")
60 static GstStaticPadTemplate gst_rtp_L16depay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("application/x-rtp")
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);
71 static void gst_rtp_L16depay_chain (GstPad * pad, GstData * _data);
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 *
80 static GstElementClass *parent_class = NULL;
83 gst_rtp_L16depay_get_type (void)
85 static GType rtpL16depay_type = 0;
87 if (!rtpL16depay_type) {
88 static const GTypeInfo rtpL16depay_info = {
89 sizeof (GstRtpL16DepayClass),
90 (GBaseInitFunc) gst_rtp_L16depay_base_init,
92 (GClassInitFunc) gst_rtp_L16depay_class_init,
95 sizeof (GstRtpL16Depay),
97 (GInstanceInitFunc) gst_rtp_L16depay_init,
101 g_type_register_static (GST_TYPE_ELEMENT, "GstRtpL16Depay",
102 &rtpL16depay_info, 0);
104 return rtpL16depay_type;
108 gst_rtp_L16depay_base_init (GstRtpL16DepayClass * klass)
110 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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);
120 gst_rtp_L16depay_class_init (GstRtpL16DepayClass * klass)
122 GObjectClass *gobject_class;
123 GstElementClass *gstelement_class;
125 gobject_class = (GObjectClass *) klass;
126 gstelement_class = (GstElementClass *) klass;
128 parent_class = g_type_class_peek_parent (klass);
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));
137 gobject_class->set_property = gst_rtp_L16depay_set_property;
138 gobject_class->get_property = gst_rtp_L16depay_get_property;
140 gstelement_class->change_state = gst_rtp_L16depay_change_state;
144 gst_rtp_L16depay_init (GstRtpL16Depay * rtpL16depay)
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,
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);
155 rtpL16depay->frequency = 44100;
156 rtpL16depay->channels = 2;
158 rtpL16depay->payload_type = PAYLOAD_L16_STEREO;
162 gst_rtp_L16depay_ntohs (GstBuffer * buf)
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 *);
170 for (; i < len; i++) {
176 gst_rtp_L16_caps_nego (GstRtpL16Depay * rtpL16depay)
181 gst_caps_copy (gst_static_caps_get (&gst_rtp_L16depay_src_template.
184 gst_caps_set_simple (caps,
185 "rate", G_TYPE_INT, rtpL16depay->frequency,
186 "channel", G_TYPE_INT, rtpL16depay->channels, NULL);
188 gst_pad_try_set_caps (rtpL16depay->srcpad, caps);
192 gst_rtp_L16depay_payloadtype_change (GstRtpL16Depay * rtpL16depay,
195 rtpL16depay->payload_type = pt;
198 case PAYLOAD_L16_MONO:
199 rtpL16depay->channels = 1;
201 case PAYLOAD_L16_STEREO:
202 rtpL16depay->channels = 2;
205 g_warning ("unknown payload_t %d\n", pt);
208 gst_rtp_L16_caps_nego (rtpL16depay);
212 gst_rtp_L16depay_chain (GstPad * pad, GstData * _data)
214 GstBuffer *buf = GST_BUFFER (_data);
215 GstRtpL16Depay *rtpL16depay;
220 g_return_if_fail (pad != NULL);
221 g_return_if_fail (GST_IS_PAD (pad));
222 g_return_if_fail (buf != NULL);
224 rtpL16depay = GST_RTP_L16_DEPAY (GST_OBJECT_PARENT (pad));
226 g_return_if_fail (rtpL16depay != NULL);
227 g_return_if_fail (GST_IS_RTP_L16_DEPAY (rtpL16depay));
229 if (GST_IS_EVENT (buf)) {
230 GstEvent *event = GST_EVENT (buf);
232 gst_pad_event_default (pad, event);
237 if (GST_PAD_CAPS (rtpL16depay->srcpad) == NULL) {
238 gst_rtp_L16_caps_nego (rtpL16depay);
242 rtp_packet_new_copy_data (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
244 pt = rtp_packet_get_payload_type (packet);
246 if (pt != rtpL16depay->payload_type) {
247 gst_rtp_L16depay_payloadtype_change (rtpL16depay, pt);
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;
256 memcpy (GST_BUFFER_DATA (outbuf), rtp_packet_get_payload (packet),
257 GST_BUFFER_SIZE (outbuf));
259 GST_DEBUG ("gst_rtp_L16depay_chain: pushing buffer of size %d",
260 GST_BUFFER_SIZE (outbuf));
262 /* FIXME: According to RFC 1890, this is required, right? */
263 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
264 gst_rtp_L16depay_ntohs (outbuf);
267 gst_pad_push (rtpL16depay->srcpad, GST_DATA (outbuf));
269 rtp_packet_free (packet);
270 gst_buffer_unref (buf);
274 gst_rtp_L16depay_set_property (GObject * object, guint prop_id,
275 const GValue * value, GParamSpec * pspec)
277 GstRtpL16Depay *rtpL16depay;
279 g_return_if_fail (GST_IS_RTP_L16_DEPAY (object));
280 rtpL16depay = GST_RTP_L16_DEPAY (object);
283 case ARG_PAYLOAD_TYPE:
284 gst_rtp_L16depay_payloadtype_change (rtpL16depay,
285 g_value_get_int (value));
288 rtpL16depay->frequency = g_value_get_int (value);
296 gst_rtp_L16depay_get_property (GObject * object, guint prop_id, GValue * value,
299 GstRtpL16Depay *rtpL16depay;
301 g_return_if_fail (GST_IS_RTP_L16_DEPAY (object));
302 rtpL16depay = GST_RTP_L16_DEPAY (object);
305 case ARG_PAYLOAD_TYPE:
306 g_value_set_int (value, rtpL16depay->payload_type);
309 g_value_set_int (value, rtpL16depay->frequency);
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317 static GstStateChangeReturn
318 gst_rtp_L16depay_change_state (GstElement * element, GstStateChange transition)
320 GstRtpL16Depay *rtpL16depay;
321 GstStateChangeReturn ret;
323 rtpL16depay = GST_RTP_L16_DEPAY (element);
325 GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
328 switch (transition) {
329 case GST_STATE_CHANGE_NULL_TO_READY:
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);
337 switch (transition) {
338 case GST_STATE_CHANGE_READY_TO_NULL:
348 gst_rtp_L16depay_plugin_init (GstPlugin * plugin)
350 return gst_element_register (plugin, "rtpL16depay",
351 GST_RANK_NONE, GST_TYPE_RTP_L16_DEPAY);