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
20 #include "gstrtpgsmparse.h"
21 #include "gstrtp-common.h"
23 /* elementfactory information */
24 static GstElementDetails gst_rtp_gsmparse_details = {
28 "Extracts GSM audio from RTP packets",
30 "Zeeshan Ali <zak147@yahoo.com>",
34 /* RtpGSMParse signals and args */
47 GST_PAD_TEMPLATE_FACTORY (src_factory,
54 "rate", GST_PROPS_INT_RANGE (1000, 48000))
57 GST_PAD_TEMPLATE_FACTORY (sink_factory,
67 static void gst_rtpgsmparse_class_init (GstRtpGSMParseClass * klass);
68 static void gst_rtpgsmparse_init (GstRtpGSMParse * rtpgsmparse);
70 static void gst_rtpgsmparse_chain (GstPad * pad, GstData *_data);
72 static void gst_rtpgsmparse_set_property (GObject * object, guint prop_id,
73 const GValue * value, GParamSpec * pspec);
74 static void gst_rtpgsmparse_get_property (GObject * object, guint prop_id,
75 GValue * value, GParamSpec * pspec);
76 static GstElementStateReturn gst_rtpgsmparse_change_state (GstElement * element);
78 static GstElementClass *parent_class = NULL;
80 static GType gst_rtpgsmparse_get_type (void)
82 static GType rtpgsmparse_type = 0;
84 if (!rtpgsmparse_type) {
85 static const GTypeInfo rtpgsmparse_info = {
86 sizeof (GstRtpGSMParseClass),
89 (GClassInitFunc) gst_rtpgsmparse_class_init,
92 sizeof (GstRtpGSMParse),
94 (GInstanceInitFunc) gst_rtpgsmparse_init,
97 rtpgsmparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstRtpGSMParse", &rtpgsmparse_info, 0);
99 return rtpgsmparse_type;
103 gst_rtpgsmparse_class_init (GstRtpGSMParseClass * klass)
105 GObjectClass *gobject_class;
106 GstElementClass *gstelement_class;
108 gobject_class = (GObjectClass *) klass;
109 gstelement_class = (GstElementClass *) klass;
111 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
113 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREQUENCY,
114 g_param_spec_int ("frequency", "frequency", "frequency",
115 G_MININT, G_MAXINT, 8000, G_PARAM_READWRITE));
117 gobject_class->set_property = gst_rtpgsmparse_set_property;
118 gobject_class->get_property = gst_rtpgsmparse_get_property;
120 gstelement_class->change_state = gst_rtpgsmparse_change_state;
124 gst_rtpgsmparse_init (GstRtpGSMParse * rtpgsmparse)
126 rtpgsmparse->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (src_factory), "src");
127 rtpgsmparse->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_factory), "sink");
128 gst_element_add_pad (GST_ELEMENT (rtpgsmparse), rtpgsmparse->srcpad);
129 gst_element_add_pad (GST_ELEMENT (rtpgsmparse), rtpgsmparse->sinkpad);
130 gst_pad_set_chain_function (rtpgsmparse->sinkpad, gst_rtpgsmparse_chain);
132 rtpgsmparse->frequency = 8000;
136 gst_rtpgsmparse_ntohs (GstBuffer *buf)
140 /* FIXME: is this code correct or even sane at all? */
141 i = (gint16 *) GST_BUFFER_DATA(buf);
142 len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
150 gst_rtpgsm_caps_nego (GstRtpGSMParse *rtpgsmparse)
154 caps = GST_CAPS_NEW (
157 "rate", GST_PROPS_INT (rtpgsmparse->frequency));
159 gst_pad_try_set_caps (rtpgsmparse->srcpad, caps);
163 gst_rtpgsmparse_chain (GstPad * pad, GstData *_data)
165 GstBuffer *buf = GST_BUFFER (_data);
166 GstRtpGSMParse *rtpgsmparse;
171 g_return_if_fail (pad != NULL);
172 g_return_if_fail (GST_IS_PAD (pad));
173 g_return_if_fail (buf != NULL);
175 rtpgsmparse = GST_RTP_GSM_PARSE (GST_OBJECT_PARENT (pad));
177 g_return_if_fail (rtpgsmparse != NULL);
178 g_return_if_fail (GST_IS_RTP_GSM_PARSE (rtpgsmparse));
180 if (GST_IS_EVENT (buf)) {
181 GstEvent *event = GST_EVENT (buf);
182 gst_pad_event_default (pad, event);
187 if (GST_PAD_CAPS (rtpgsmparse->srcpad) == NULL) {
188 gst_rtpgsm_caps_nego (rtpgsmparse);
191 packet = rtp_packet_new_copy_data (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
193 pt = rtp_packet_get_payload_type (packet);
195 if (pt != PAYLOAD_GSM) {
196 g_warning ("Unexpected paload type %u\n", pt);
197 rtp_packet_free (packet);
198 gst_buffer_unref (buf);
202 outbuf = gst_buffer_new ();
203 GST_BUFFER_SIZE (outbuf) = rtp_packet_get_payload_len (packet);
204 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
205 GST_BUFFER_TIMESTAMP (outbuf) = g_ntohl (rtp_packet_get_timestamp (packet)) * GST_SECOND;
207 memcpy (GST_BUFFER_DATA (outbuf), rtp_packet_get_payload (packet), GST_BUFFER_SIZE (outbuf));
209 GST_DEBUG ("gst_rtpgsmparse_chain: pushing buffer of size %d", GST_BUFFER_SIZE(outbuf));
211 /* FIXME: According to RFC 1890, this is required, right? */
212 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
213 gst_rtpgsmparse_ntohs (outbuf);
216 gst_pad_push (rtpgsmparse->srcpad, GST_DATA (outbuf));
218 rtp_packet_free (packet);
219 gst_buffer_unref (buf);
223 gst_rtpgsmparse_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
225 GstRtpGSMParse *rtpgsmparse;
227 /* it's not null if we got it, but it might not be ours */
228 g_return_if_fail (GST_IS_RTP_GSM_PARSE (object));
229 rtpgsmparse = GST_RTP_GSM_PARSE (object);
233 rtpgsmparse->frequency = g_value_get_int (value);
241 gst_rtpgsmparse_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
243 GstRtpGSMParse *rtpgsmparse;
245 /* it's not null if we got it, but it might not be ours */
246 g_return_if_fail (GST_IS_RTP_GSM_PARSE (object));
247 rtpgsmparse = GST_RTP_GSM_PARSE (object);
251 g_value_set_int (value, rtpgsmparse->frequency);
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 static GstElementStateReturn
260 gst_rtpgsmparse_change_state (GstElement * element)
262 GstRtpGSMParse *rtpgsmparse;
264 g_return_val_if_fail (GST_IS_RTP_GSM_PARSE (element), GST_STATE_FAILURE);
266 rtpgsmparse = GST_RTP_GSM_PARSE (element);
268 GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
270 switch (GST_STATE_TRANSITION (element)) {
271 case GST_STATE_NULL_TO_READY:
273 case GST_STATE_READY_TO_NULL:
279 /* if we haven't failed already, give the parent class a chance to ;-) */
280 if (GST_ELEMENT_CLASS (parent_class)->change_state)
281 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
283 return GST_STATE_SUCCESS;
287 gst_rtpgsmparse_plugin_init (GModule * module, GstPlugin * plugin)
289 GstElementFactory *rtpgsmparse;
291 rtpgsmparse = gst_element_factory_new ("rtpgsmparse", GST_TYPE_RTP_GSM_PARSE, &gst_rtp_gsmparse_details);
292 g_return_val_if_fail (rtpgsmparse != NULL, FALSE);
294 gst_element_factory_add_pad_template (rtpgsmparse, GST_PAD_TEMPLATE_GET (src_factory));
295 gst_element_factory_add_pad_template (rtpgsmparse, GST_PAD_TEMPLATE_GET (sink_factory));
297 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (rtpgsmparse));