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.
25 #include "gstrtpgsmenc.h"
27 /* elementfactory information */
28 static GstElementDetails gst_rtpgsmenc_details = {
29 "RTP GSM Audio Encoder",
32 "Encodes GSM audio into an RTP packet",
34 "Zeeshan Ali <zak147@yahoo.com>",
38 /* RtpGSMEnc signals and args */
51 GST_PAD_TEMPLATE_FACTORY (sink_factory,
58 "rate", GST_PROPS_INT_RANGE (1000, 48000)
62 GST_PAD_TEMPLATE_FACTORY (src_factory,
72 static void gst_rtpgsmenc_class_init (GstRtpGSMEncClass * klass);
73 static void gst_rtpgsmenc_init (GstRtpGSMEnc * rtpgsmenc);
74 static void gst_rtpgsmenc_chain (GstPad * pad, GstData *_data);
75 static void gst_rtpgsmenc_set_property (GObject * object, guint prop_id,
76 const GValue * value, GParamSpec * pspec);
77 static void gst_rtpgsmenc_get_property (GObject * object, guint prop_id,
78 GValue * value, GParamSpec * pspec);
79 static GstPadLinkReturn gst_rtpgsmenc_sinkconnect (GstPad * pad, GstCaps * caps);
80 static GstElementStateReturn gst_rtpgsmenc_change_state (GstElement * element);
82 static GstElementClass *parent_class = NULL;
84 static GType gst_rtpgsmenc_get_type (void)
86 static GType rtpgsmenc_type = 0;
88 if (!rtpgsmenc_type) {
89 static const GTypeInfo rtpgsmenc_info = {
90 sizeof (GstRtpGSMEncClass),
93 (GClassInitFunc) gst_rtpgsmenc_class_init,
96 sizeof (GstRtpGSMEnc),
98 (GInstanceInitFunc) gst_rtpgsmenc_init,
101 rtpgsmenc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstRtpGSMEnc", &rtpgsmenc_info, 0);
103 return rtpgsmenc_type;
107 gst_rtpgsmenc_class_init (GstRtpGSMEncClass * klass)
109 GObjectClass *gobject_class;
110 GstElementClass *gstelement_class;
112 gobject_class = (GObjectClass *) klass;
113 gstelement_class = (GstElementClass *) klass;
115 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
117 gobject_class->set_property = gst_rtpgsmenc_set_property;
118 gobject_class->get_property = gst_rtpgsmenc_get_property;
120 gstelement_class->change_state = gst_rtpgsmenc_change_state;
124 gst_rtpgsmenc_init (GstRtpGSMEnc * rtpgsmenc)
126 rtpgsmenc->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_factory), "sink");
127 rtpgsmenc->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (src_factory), "src");
128 gst_element_add_pad (GST_ELEMENT (rtpgsmenc), rtpgsmenc->sinkpad);
129 gst_element_add_pad (GST_ELEMENT (rtpgsmenc), rtpgsmenc->srcpad);
130 gst_pad_set_chain_function (rtpgsmenc->sinkpad, gst_rtpgsmenc_chain);
131 gst_pad_set_link_function (rtpgsmenc->sinkpad, gst_rtpgsmenc_sinkconnect);
133 rtpgsmenc->frequency = 8000;
135 rtpgsmenc->next_time = 0;
136 rtpgsmenc->time_interval = 0;
139 rtpgsmenc->ssrc = random ();
142 static GstPadLinkReturn
143 gst_rtpgsmenc_sinkconnect (GstPad * pad, GstCaps * caps)
145 GstRtpGSMEnc *rtpgsmenc;
147 rtpgsmenc = GST_RTP_GSM_ENC (gst_pad_get_parent (pad));
149 gst_caps_get_int (caps, "rate", &rtpgsmenc->frequency);
151 /* Pre-calculate what we can */
152 rtpgsmenc->time_interval = GST_SECOND / (2 * rtpgsmenc->frequency);
154 return GST_PAD_LINK_OK;
159 gst_rtpgsmenc_htons (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 *);
173 gst_rtpgsmenc_chain (GstPad * pad, GstData *_data)
175 GstBuffer *buf = GST_BUFFER (_data);
176 GstRtpGSMEnc *rtpgsmenc;
180 g_return_if_fail (pad != NULL);
181 g_return_if_fail (GST_IS_PAD (pad));
182 g_return_if_fail (buf != NULL);
184 rtpgsmenc = GST_RTP_GSM_ENC (GST_OBJECT_PARENT (pad));
186 g_return_if_fail (rtpgsmenc != NULL);
187 g_return_if_fail (GST_IS_RTP_GSM_ENC (rtpgsmenc));
189 if (GST_IS_EVENT (buf)) {
190 GstEvent *event = GST_EVENT (buf);
192 switch (GST_EVENT_TYPE (event)) {
193 case GST_EVENT_DISCONTINUOUS:
194 GST_DEBUG ("discont");
195 rtpgsmenc->next_time = 0;
196 gst_pad_event_default (pad, event);
199 gst_pad_event_default (pad, event);
204 /* We only need the header */
205 packet = rtp_packet_new_allocate (0, 0, 0);
207 rtp_packet_set_csrc_count (packet, 0);
208 rtp_packet_set_extension (packet, 0);
209 rtp_packet_set_padding (packet, 0);
210 rtp_packet_set_version (packet, RTP_VERSION);
211 rtp_packet_set_marker (packet, 0);
212 rtp_packet_set_ssrc (packet, g_htonl (rtpgsmenc->ssrc));
213 rtp_packet_set_seq (packet, g_htons (rtpgsmenc->seq));
214 rtp_packet_set_timestamp (packet, g_htonl ((guint32) rtpgsmenc->next_time / GST_SECOND));
215 rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_GSM);
217 /* FIXME: According to RFC 1890, this is required, right? */
218 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
219 gst_rtpgsmenc_htons (buf);
222 outbuf = gst_buffer_new ();
223 GST_BUFFER_SIZE (outbuf) = rtp_packet_get_packet_len (packet) + GST_BUFFER_SIZE (buf);
224 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
225 GST_BUFFER_TIMESTAMP (outbuf) = rtpgsmenc->next_time;
227 memcpy (GST_BUFFER_DATA (outbuf), packet->data, rtp_packet_get_packet_len (packet));
228 memcpy (GST_BUFFER_DATA (outbuf) + rtp_packet_get_packet_len(packet), GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
230 GST_DEBUG ("gst_rtpgsmenc_chain: pushing buffer of size %d", GST_BUFFER_SIZE(outbuf));
231 gst_pad_push (rtpgsmenc->srcpad, GST_DATA (outbuf));
234 rtpgsmenc->next_time += rtpgsmenc->time_interval * GST_BUFFER_SIZE (buf);
236 rtp_packet_free (packet);
237 gst_buffer_unref (buf);
241 gst_rtpgsmenc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
243 GstRtpGSMEnc *rtpgsmenc;
245 /* it's not null if we got it, but it might not be ours */
246 g_return_if_fail (GST_IS_RTP_GSM_ENC (object));
247 rtpgsmenc = GST_RTP_GSM_ENC (object);
256 gst_rtpgsmenc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
258 GstRtpGSMEnc *rtpgsmenc;
260 /* it's not null if we got it, but it might not be ours */
261 g_return_if_fail (GST_IS_RTP_GSM_ENC (object));
262 rtpgsmenc = GST_RTP_GSM_ENC (object);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271 static GstElementStateReturn
272 gst_rtpgsmenc_change_state (GstElement * element)
274 GstRtpGSMEnc *rtpgsmenc;
276 g_return_val_if_fail (GST_IS_RTP_GSM_ENC (element), GST_STATE_FAILURE);
278 rtpgsmenc = GST_RTP_GSM_ENC (element);
280 GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
282 /* if going down into NULL state, close the file if it's open */
283 switch (GST_STATE_TRANSITION (element)) {
284 case GST_STATE_NULL_TO_READY:
287 case GST_STATE_READY_TO_NULL:
294 /* if we haven't failed already, give the parent class a chance to ;-) */
295 if (GST_ELEMENT_CLASS (parent_class)->change_state)
296 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
298 return GST_STATE_SUCCESS;
302 gst_rtpgsmenc_plugin_init (GModule * module, GstPlugin * plugin)
304 GstElementFactory *rtpgsmenc;
306 rtpgsmenc = gst_element_factory_new ("rtpgsmenc", GST_TYPE_RTP_GSM_ENC, &gst_rtpgsmenc_details);
307 g_return_val_if_fail (rtpgsmenc != NULL, FALSE);
309 gst_element_factory_add_pad_template (rtpgsmenc, GST_PAD_TEMPLATE_GET (sink_factory));
310 gst_element_factory_add_pad_template (rtpgsmenc, GST_PAD_TEMPLATE_GET (src_factory));
312 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (rtpgsmenc));