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
19 #include "gstrtpL16parse.h"
20 #include "gstrtp-common.h"
22 /* elementfactory information */
23 static GstElementDetails gst_rtp_L16parse_details = {
25 "Codec/Parser/Network",
26 "Extracts raw audio from RTP packets",
27 "Zeeshan Ali <zak147@yahoo.com>"
30 /* RtpL16Parse signals and args */
44 static GstStaticPadTemplate gst_rtpL16parse_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
48 GST_STATIC_CAPS ("audio/x-raw-int, "
49 "endianness = (int) BYTE_ORDER, "
50 "signed = (boolean) true, "
53 "rate = (int) [ 1000, 48000 ], " "channels = (int) [ 1, 2 ]")
56 static GstStaticPadTemplate gst_rtpL16parse_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS ("application/x-rtp")
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);
67 static void gst_rtpL16parse_chain (GstPad * pad, GstData * _data);
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 *
76 static GstElementClass *parent_class = NULL;
79 gst_rtpL16parse_get_type (void)
81 static GType rtpL16parse_type = 0;
83 if (!rtpL16parse_type) {
84 static const GTypeInfo rtpL16parse_info = {
85 sizeof (GstRtpL16ParseClass),
86 (GBaseInitFunc) gst_rtpL16parse_base_init,
88 (GClassInitFunc) gst_rtpL16parse_class_init,
91 sizeof (GstRtpL16Parse),
93 (GInstanceInitFunc) gst_rtpL16parse_init,
97 g_type_register_static (GST_TYPE_ELEMENT, "GstRtpL16Parse",
98 &rtpL16parse_info, 0);
100 return rtpL16parse_type;
104 gst_rtpL16parse_base_init (GstRtpL16ParseClass * klass)
106 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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);
116 gst_rtpL16parse_class_init (GstRtpL16ParseClass * klass)
118 GObjectClass *gobject_class;
119 GstElementClass *gstelement_class;
121 gobject_class = (GObjectClass *) klass;
122 gstelement_class = (GstElementClass *) klass;
124 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
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));
133 gobject_class->set_property = gst_rtpL16parse_set_property;
134 gobject_class->get_property = gst_rtpL16parse_get_property;
136 gstelement_class->change_state = gst_rtpL16parse_change_state;
140 gst_rtpL16parse_init (GstRtpL16Parse * rtpL16parse)
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);
152 rtpL16parse->frequency = 44100;
153 rtpL16parse->channels = 2;
155 rtpL16parse->payload_type = PAYLOAD_L16_STEREO;
159 gst_rtpL16parse_ntohs (GstBuffer * buf)
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 *);
167 for (; i < len; i++) {
173 gst_rtpL16_caps_nego (GstRtpL16Parse * rtpL16parse)
178 gst_caps_copy (gst_static_caps_get (&gst_rtpL16parse_src_template.
181 gst_caps_set_simple (caps,
182 "rate", G_TYPE_INT, rtpL16parse->frequency,
183 "channel", G_TYPE_INT, rtpL16parse->channels, NULL);
185 gst_pad_try_set_caps (rtpL16parse->srcpad, caps);
189 gst_rtpL16parse_payloadtype_change (GstRtpL16Parse * rtpL16parse,
192 rtpL16parse->payload_type = pt;
195 case PAYLOAD_L16_MONO:
196 rtpL16parse->channels = 1;
198 case PAYLOAD_L16_STEREO:
199 rtpL16parse->channels = 2;
202 g_warning ("unknown payload_t %d\n", pt);
205 gst_rtpL16_caps_nego (rtpL16parse);
209 gst_rtpL16parse_chain (GstPad * pad, GstData * _data)
211 GstBuffer *buf = GST_BUFFER (_data);
212 GstRtpL16Parse *rtpL16parse;
217 g_return_if_fail (pad != NULL);
218 g_return_if_fail (GST_IS_PAD (pad));
219 g_return_if_fail (buf != NULL);
221 rtpL16parse = GST_RTP_L16_PARSE (GST_OBJECT_PARENT (pad));
223 g_return_if_fail (rtpL16parse != NULL);
224 g_return_if_fail (GST_IS_RTP_L16_PARSE (rtpL16parse));
226 if (GST_IS_EVENT (buf)) {
227 GstEvent *event = GST_EVENT (buf);
229 gst_pad_event_default (pad, event);
234 if (GST_PAD_CAPS (rtpL16parse->srcpad) == NULL) {
235 gst_rtpL16_caps_nego (rtpL16parse);
239 rtp_packet_new_copy_data (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
241 pt = rtp_packet_get_payload_type (packet);
243 if (pt != rtpL16parse->payload_type) {
244 gst_rtpL16parse_payloadtype_change (rtpL16parse, pt);
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;
253 memcpy (GST_BUFFER_DATA (outbuf), rtp_packet_get_payload (packet),
254 GST_BUFFER_SIZE (outbuf));
256 GST_DEBUG ("gst_rtpL16parse_chain: pushing buffer of size %d",
257 GST_BUFFER_SIZE (outbuf));
259 /* FIXME: According to RFC 1890, this is required, right? */
260 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
261 gst_rtpL16parse_ntohs (outbuf);
264 gst_pad_push (rtpL16parse->srcpad, GST_DATA (outbuf));
266 rtp_packet_free (packet);
267 gst_buffer_unref (buf);
271 gst_rtpL16parse_set_property (GObject * object, guint prop_id,
272 const GValue * value, GParamSpec * pspec)
274 GstRtpL16Parse *rtpL16parse;
276 g_return_if_fail (GST_IS_RTP_L16_PARSE (object));
277 rtpL16parse = GST_RTP_L16_PARSE (object);
280 case ARG_PAYLOAD_TYPE:
281 gst_rtpL16parse_payloadtype_change (rtpL16parse, g_value_get_int (value));
284 rtpL16parse->frequency = g_value_get_int (value);
292 gst_rtpL16parse_get_property (GObject * object, guint prop_id, GValue * value,
295 GstRtpL16Parse *rtpL16parse;
297 g_return_if_fail (GST_IS_RTP_L16_PARSE (object));
298 rtpL16parse = GST_RTP_L16_PARSE (object);
301 case ARG_PAYLOAD_TYPE:
302 g_value_set_int (value, rtpL16parse->payload_type);
305 g_value_set_int (value, rtpL16parse->frequency);
308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313 static GstElementStateReturn
314 gst_rtpL16parse_change_state (GstElement * element)
316 GstRtpL16Parse *rtpL16parse;
318 g_return_val_if_fail (GST_IS_RTP_L16_PARSE (element), GST_STATE_FAILURE);
320 rtpL16parse = GST_RTP_L16_PARSE (element);
322 GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
324 switch (GST_STATE_TRANSITION (element)) {
325 case GST_STATE_NULL_TO_READY:
327 case GST_STATE_READY_TO_NULL:
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);
337 return GST_STATE_SUCCESS;
341 gst_rtpL16parse_plugin_init (GstPlugin * plugin)
343 return gst_element_register (plugin, "rtpL16parse",
344 GST_RANK_NONE, GST_TYPE_RTP_L16_PARSE);