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 = {
26 "Codec/Parser/Network",
27 "Extracts GSM audio from RTP packets",
28 "Zeeshan Ali <zak147@yahoo.com>"
31 /* RtpGSMParse signals and args */
44 static GstStaticPadTemplate gst_rtpgsmparse_src_template =
45 GST_STATIC_PAD_TEMPLATE (
49 GST_STATIC_CAPS ( "audio/x-gsm, "
50 "rate = (int) [ 1000, 48000 ]"
54 static GstStaticPadTemplate gst_rtpgsmparse_sink_template =
55 GST_STATIC_PAD_TEMPLATE (
59 GST_STATIC_CAPS ("application/x-rtp")
63 static void gst_rtpgsmparse_class_init (GstRtpGSMParseClass * klass);
64 static void gst_rtpgsmparse_base_init (GstRtpGSMParseClass * klass);
65 static void gst_rtpgsmparse_init (GstRtpGSMParse * rtpgsmparse);
67 static void gst_rtpgsmparse_chain (GstPad * pad, GstData *_data);
69 static void gst_rtpgsmparse_set_property (GObject * object, guint prop_id,
70 const GValue * value, GParamSpec * pspec);
71 static void gst_rtpgsmparse_get_property (GObject * object, guint prop_id,
72 GValue * value, GParamSpec * pspec);
73 static GstElementStateReturn gst_rtpgsmparse_change_state (GstElement * element);
75 static GstElementClass *parent_class = NULL;
77 static GType gst_rtpgsmparse_get_type (void)
79 static GType rtpgsmparse_type = 0;
81 if (!rtpgsmparse_type) {
82 static const GTypeInfo rtpgsmparse_info = {
83 sizeof (GstRtpGSMParseClass),
84 (GBaseInitFunc) gst_rtpgsmparse_base_init,
86 (GClassInitFunc) gst_rtpgsmparse_class_init,
89 sizeof (GstRtpGSMParse),
91 (GInstanceInitFunc) gst_rtpgsmparse_init,
94 rtpgsmparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstRtpGSMParse", &rtpgsmparse_info, 0);
96 return rtpgsmparse_type;
100 gst_rtpgsmparse_base_init (GstRtpGSMParseClass * klass)
102 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
104 gst_element_class_add_pad_template (element_class,
105 gst_static_pad_template_get(&gst_rtpgsmparse_src_template));
106 gst_element_class_add_pad_template (element_class,
107 gst_static_pad_template_get(&gst_rtpgsmparse_sink_template));
108 gst_element_class_set_details (element_class, &gst_rtp_gsmparse_details);
112 gst_rtpgsmparse_class_init (GstRtpGSMParseClass * klass)
114 GObjectClass *gobject_class;
115 GstElementClass *gstelement_class;
117 gobject_class = (GObjectClass *) klass;
118 gstelement_class = (GstElementClass *) klass;
120 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
122 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREQUENCY,
123 g_param_spec_int ("frequency", "frequency", "frequency",
124 G_MININT, G_MAXINT, 8000, G_PARAM_READWRITE));
126 gobject_class->set_property = gst_rtpgsmparse_set_property;
127 gobject_class->get_property = gst_rtpgsmparse_get_property;
129 gstelement_class->change_state = gst_rtpgsmparse_change_state;
133 gst_rtpgsmparse_init (GstRtpGSMParse * rtpgsmparse)
135 rtpgsmparse->srcpad = gst_pad_new_from_template (
136 gst_static_pad_template_get(&gst_rtpgsmparse_src_template), "src");
137 rtpgsmparse->sinkpad = gst_pad_new_from_template (
138 gst_static_pad_template_get(&gst_rtpgsmparse_sink_template), "sink");
139 gst_element_add_pad (GST_ELEMENT (rtpgsmparse), rtpgsmparse->srcpad);
140 gst_element_add_pad (GST_ELEMENT (rtpgsmparse), rtpgsmparse->sinkpad);
141 gst_pad_set_chain_function (rtpgsmparse->sinkpad, gst_rtpgsmparse_chain);
143 rtpgsmparse->frequency = 8000;
147 gst_rtpgsmparse_ntohs (GstBuffer *buf)
151 /* FIXME: is this code correct or even sane at all? */
152 i = (gint16 *) GST_BUFFER_DATA(buf);
153 len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
161 gst_rtpgsm_caps_nego (GstRtpGSMParse *rtpgsmparse)
165 caps = gst_caps_new_simple ("audio/x-gsm",
166 "rate", G_TYPE_INT, rtpgsmparse->frequency);
168 gst_pad_try_set_caps (rtpgsmparse->srcpad, caps);
172 gst_rtpgsmparse_chain (GstPad * pad, GstData *_data)
174 GstBuffer *buf = GST_BUFFER (_data);
175 GstRtpGSMParse *rtpgsmparse;
180 g_return_if_fail (pad != NULL);
181 g_return_if_fail (GST_IS_PAD (pad));
182 g_return_if_fail (buf != NULL);
184 rtpgsmparse = GST_RTP_GSM_PARSE (GST_OBJECT_PARENT (pad));
186 g_return_if_fail (rtpgsmparse != NULL);
187 g_return_if_fail (GST_IS_RTP_GSM_PARSE (rtpgsmparse));
189 if (GST_IS_EVENT (buf)) {
190 GstEvent *event = GST_EVENT (buf);
191 gst_pad_event_default (pad, event);
196 if (GST_PAD_CAPS (rtpgsmparse->srcpad) == NULL) {
197 gst_rtpgsm_caps_nego (rtpgsmparse);
200 packet = rtp_packet_new_copy_data (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
202 pt = rtp_packet_get_payload_type (packet);
204 if (pt != PAYLOAD_GSM) {
205 g_warning ("Unexpected paload type %u\n", pt);
206 rtp_packet_free (packet);
207 gst_buffer_unref (buf);
211 outbuf = gst_buffer_new ();
212 GST_BUFFER_SIZE (outbuf) = rtp_packet_get_payload_len (packet);
213 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
214 GST_BUFFER_TIMESTAMP (outbuf) = g_ntohl (rtp_packet_get_timestamp (packet)) * GST_SECOND;
216 memcpy (GST_BUFFER_DATA (outbuf), rtp_packet_get_payload (packet), GST_BUFFER_SIZE (outbuf));
218 GST_DEBUG ("gst_rtpgsmparse_chain: pushing buffer of size %d", GST_BUFFER_SIZE(outbuf));
220 /* FIXME: According to RFC 1890, this is required, right? */
221 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
222 gst_rtpgsmparse_ntohs (outbuf);
225 gst_pad_push (rtpgsmparse->srcpad, GST_DATA (outbuf));
227 rtp_packet_free (packet);
228 gst_buffer_unref (buf);
232 gst_rtpgsmparse_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
234 GstRtpGSMParse *rtpgsmparse;
236 /* it's not null if we got it, but it might not be ours */
237 g_return_if_fail (GST_IS_RTP_GSM_PARSE (object));
238 rtpgsmparse = GST_RTP_GSM_PARSE (object);
242 rtpgsmparse->frequency = g_value_get_int (value);
250 gst_rtpgsmparse_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
252 GstRtpGSMParse *rtpgsmparse;
254 /* it's not null if we got it, but it might not be ours */
255 g_return_if_fail (GST_IS_RTP_GSM_PARSE (object));
256 rtpgsmparse = GST_RTP_GSM_PARSE (object);
260 g_value_set_int (value, rtpgsmparse->frequency);
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 static GstElementStateReturn
269 gst_rtpgsmparse_change_state (GstElement * element)
271 GstRtpGSMParse *rtpgsmparse;
273 g_return_val_if_fail (GST_IS_RTP_GSM_PARSE (element), GST_STATE_FAILURE);
275 rtpgsmparse = GST_RTP_GSM_PARSE (element);
277 GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
279 switch (GST_STATE_TRANSITION (element)) {
280 case GST_STATE_NULL_TO_READY:
282 case GST_STATE_READY_TO_NULL:
288 /* if we haven't failed already, give the parent class a chance to ;-) */
289 if (GST_ELEMENT_CLASS (parent_class)->change_state)
290 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
292 return GST_STATE_SUCCESS;
296 gst_rtpgsmparse_plugin_init (GstPlugin * plugin)
298 return gst_element_register (plugin, "rtpgsmparse",
299 GST_RANK_NONE, GST_TYPE_RTP_GSM_PARSE);