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",
30 "Codec/Encoder/Network",
31 "Encodes GSM audio into an RTP packet",
32 "Zeeshan Ali <zak147@yahoo.com>"
35 /* RtpGSMEnc signals and args */
48 static GstStaticPadTemplate gst_rtpgsmenc_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS ("audio/x-gsm, " "rate = (int) [ 1000, 48000 ]")
55 static GstStaticPadTemplate gst_rtpgsmenc_src_template =
56 GST_STATIC_PAD_TEMPLATE ("src",
59 GST_STATIC_CAPS ("application/x-rtp")
63 static void gst_rtpgsmenc_class_init (GstRtpGSMEncClass * klass);
64 static void gst_rtpgsmenc_base_init (GstRtpGSMEncClass * klass);
65 static void gst_rtpgsmenc_init (GstRtpGSMEnc * rtpgsmenc);
66 static void gst_rtpgsmenc_chain (GstPad * pad, GstData * _data);
67 static void gst_rtpgsmenc_set_property (GObject * object, guint prop_id,
68 const GValue * value, GParamSpec * pspec);
69 static void gst_rtpgsmenc_get_property (GObject * object, guint prop_id,
70 GValue * value, GParamSpec * pspec);
71 static GstPadLinkReturn gst_rtpgsmenc_sinkconnect (GstPad * pad,
72 const GstCaps * caps);
73 static GstElementStateReturn gst_rtpgsmenc_change_state (GstElement * element);
75 static GstElementClass *parent_class = NULL;
78 gst_rtpgsmenc_get_type (void)
80 static GType rtpgsmenc_type = 0;
82 if (!rtpgsmenc_type) {
83 static const GTypeInfo rtpgsmenc_info = {
84 sizeof (GstRtpGSMEncClass),
85 (GBaseInitFunc) gst_rtpgsmenc_base_init,
87 (GClassInitFunc) gst_rtpgsmenc_class_init,
90 sizeof (GstRtpGSMEnc),
92 (GInstanceInitFunc) gst_rtpgsmenc_init,
96 g_type_register_static (GST_TYPE_ELEMENT, "GstRtpGSMEnc",
99 return rtpgsmenc_type;
103 gst_rtpgsmenc_base_init (GstRtpGSMEncClass * klass)
105 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
107 gst_element_class_add_pad_template (element_class,
108 gst_static_pad_template_get (&gst_rtpgsmenc_sink_template));
109 gst_element_class_add_pad_template (element_class,
110 gst_static_pad_template_get (&gst_rtpgsmenc_src_template));
111 gst_element_class_set_details (element_class, &gst_rtpgsmenc_details);
115 gst_rtpgsmenc_class_init (GstRtpGSMEncClass * klass)
117 GObjectClass *gobject_class;
118 GstElementClass *gstelement_class;
120 gobject_class = (GObjectClass *) klass;
121 gstelement_class = (GstElementClass *) klass;
123 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
125 gobject_class->set_property = gst_rtpgsmenc_set_property;
126 gobject_class->get_property = gst_rtpgsmenc_get_property;
128 gstelement_class->change_state = gst_rtpgsmenc_change_state;
132 gst_rtpgsmenc_init (GstRtpGSMEnc * rtpgsmenc)
135 gst_pad_new_from_template (gst_static_pad_template_get
136 (&gst_rtpgsmenc_sink_template), "sink");
138 gst_pad_new_from_template (gst_static_pad_template_get
139 (&gst_rtpgsmenc_sink_template), "src");
140 gst_element_add_pad (GST_ELEMENT (rtpgsmenc), rtpgsmenc->sinkpad);
141 gst_element_add_pad (GST_ELEMENT (rtpgsmenc), rtpgsmenc->srcpad);
142 gst_pad_set_chain_function (rtpgsmenc->sinkpad, gst_rtpgsmenc_chain);
143 gst_pad_set_link_function (rtpgsmenc->sinkpad, gst_rtpgsmenc_sinkconnect);
145 rtpgsmenc->frequency = 8000;
147 rtpgsmenc->next_time = 0;
148 rtpgsmenc->time_interval = 0;
151 rtpgsmenc->ssrc = random ();
154 static GstPadLinkReturn
155 gst_rtpgsmenc_sinkconnect (GstPad * pad, const GstCaps * caps)
157 GstRtpGSMEnc *rtpgsmenc;
158 GstStructure *structure;
161 rtpgsmenc = GST_RTP_GSM_ENC (gst_pad_get_parent (pad));
163 structure = gst_caps_get_structure (caps, 0);
165 ret = gst_structure_get_int (structure, "rate", &rtpgsmenc->frequency);
167 return GST_PAD_LINK_REFUSED;
169 /* Pre-calculate what we can */
170 rtpgsmenc->time_interval = GST_SECOND / (2 * rtpgsmenc->frequency);
172 return GST_PAD_LINK_OK;
177 gst_rtpgsmenc_htons (GstBuffer * buf)
181 /* FIXME: is this code correct or even sane at all? */
182 i = (gint16 *) GST_BUFFER_DATA (buf);
183 len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
185 for (; i < len; i++) {
191 gst_rtpgsmenc_chain (GstPad * pad, GstData * _data)
193 GstBuffer *buf = GST_BUFFER (_data);
194 GstRtpGSMEnc *rtpgsmenc;
198 g_return_if_fail (pad != NULL);
199 g_return_if_fail (GST_IS_PAD (pad));
200 g_return_if_fail (buf != NULL);
202 rtpgsmenc = GST_RTP_GSM_ENC (GST_OBJECT_PARENT (pad));
204 g_return_if_fail (rtpgsmenc != NULL);
205 g_return_if_fail (GST_IS_RTP_GSM_ENC (rtpgsmenc));
207 if (GST_IS_EVENT (buf)) {
208 GstEvent *event = GST_EVENT (buf);
210 switch (GST_EVENT_TYPE (event)) {
211 case GST_EVENT_DISCONTINUOUS:
212 GST_DEBUG ("discont");
213 rtpgsmenc->next_time = 0;
214 gst_pad_event_default (pad, event);
217 gst_pad_event_default (pad, event);
222 /* We only need the header */
223 packet = rtp_packet_new_allocate (0, 0, 0);
225 rtp_packet_set_csrc_count (packet, 0);
226 rtp_packet_set_extension (packet, 0);
227 rtp_packet_set_padding (packet, 0);
228 rtp_packet_set_version (packet, RTP_VERSION);
229 rtp_packet_set_marker (packet, 0);
230 rtp_packet_set_ssrc (packet, g_htonl (rtpgsmenc->ssrc));
231 rtp_packet_set_seq (packet, g_htons (rtpgsmenc->seq));
232 rtp_packet_set_timestamp (packet,
233 g_htonl ((guint32) rtpgsmenc->next_time / GST_SECOND));
234 rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_GSM);
236 /* FIXME: According to RFC 1890, this is required, right? */
237 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
238 gst_rtpgsmenc_htons (buf);
241 outbuf = gst_buffer_new ();
242 GST_BUFFER_SIZE (outbuf) =
243 rtp_packet_get_packet_len (packet) + GST_BUFFER_SIZE (buf);
244 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
245 GST_BUFFER_TIMESTAMP (outbuf) = rtpgsmenc->next_time;
247 memcpy (GST_BUFFER_DATA (outbuf), packet->data,
248 rtp_packet_get_packet_len (packet));
249 memcpy (GST_BUFFER_DATA (outbuf) + rtp_packet_get_packet_len (packet),
250 GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
252 GST_DEBUG ("gst_rtpgsmenc_chain: pushing buffer of size %d",
253 GST_BUFFER_SIZE (outbuf));
254 gst_pad_push (rtpgsmenc->srcpad, GST_DATA (outbuf));
257 rtpgsmenc->next_time += rtpgsmenc->time_interval * GST_BUFFER_SIZE (buf);
259 rtp_packet_free (packet);
260 gst_buffer_unref (buf);
264 gst_rtpgsmenc_set_property (GObject * object, guint prop_id,
265 const GValue * value, GParamSpec * pspec)
267 GstRtpGSMEnc *rtpgsmenc;
269 /* it's not null if we got it, but it might not be ours */
270 g_return_if_fail (GST_IS_RTP_GSM_ENC (object));
271 rtpgsmenc = GST_RTP_GSM_ENC (object);
280 gst_rtpgsmenc_get_property (GObject * object, guint prop_id, GValue * value,
283 GstRtpGSMEnc *rtpgsmenc;
285 /* it's not null if we got it, but it might not be ours */
286 g_return_if_fail (GST_IS_RTP_GSM_ENC (object));
287 rtpgsmenc = GST_RTP_GSM_ENC (object);
291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296 static GstElementStateReturn
297 gst_rtpgsmenc_change_state (GstElement * element)
299 GstRtpGSMEnc *rtpgsmenc;
301 g_return_val_if_fail (GST_IS_RTP_GSM_ENC (element), GST_STATE_FAILURE);
303 rtpgsmenc = GST_RTP_GSM_ENC (element);
305 GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
307 /* if going down into NULL state, close the file if it's open */
308 switch (GST_STATE_TRANSITION (element)) {
309 case GST_STATE_NULL_TO_READY:
312 case GST_STATE_READY_TO_NULL:
319 /* if we haven't failed already, give the parent class a chance to ;-) */
320 if (GST_ELEMENT_CLASS (parent_class)->change_state)
321 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
323 return GST_STATE_SUCCESS;
327 gst_rtpgsmenc_plugin_init (GstPlugin * plugin)
329 return gst_element_register (plugin, "rtpgsmenc",
330 GST_RANK_NONE, GST_TYPE_RTP_GSM_ENC);