GSM -> RTP and viceversa
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpgsmenc.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <string.h>
21 #include "gstrtpgsmenc.h"
22
23 /* elementfactory information */
24 static GstElementDetails gst_rtpgsmenc_details = {
25   "RTP GSM Audio Encoder",
26   "RtpGSMEnc",
27   "LGPL",
28   "Encodes GSM audio into an RTP packet",
29   VERSION,
30   "Zeeshan Ali <zak147@yahoo.com>",
31   "(C) 2003",
32 };
33
34 /* RtpGSMEnc signals and args */
35 enum
36 {
37   /* FILL ME */
38   LAST_SIGNAL
39 };
40
41 enum
42 {
43   /* FILL ME */
44   ARG_0,
45 };
46
47 GST_PAD_TEMPLATE_FACTORY (sink_factory,
48                 "sink",
49                 GST_PAD_SINK,
50                 GST_PAD_ALWAYS,
51                 GST_CAPS_NEW (
52                         "gsm_gsm",
53                         "audio/x-gsm",
54                         "rate",       GST_PROPS_INT_RANGE (1000, 48000)
55                 )
56 );
57
58 GST_PAD_TEMPLATE_FACTORY (src_factory,
59                 "src",
60                 GST_PAD_SRC,
61                 GST_PAD_ALWAYS,
62                 GST_CAPS_NEW (
63                         "rtp",
64                         "application/x-rtp",
65                         NULL)
66 );
67
68 static void gst_rtpgsmenc_class_init (GstRtpGSMEncClass * klass);
69 static void gst_rtpgsmenc_init (GstRtpGSMEnc * rtpgsmenc);
70 static void gst_rtpgsmenc_chain (GstPad * pad, GstBuffer * buf);
71 static void gst_rtpgsmenc_set_property (GObject * object, guint prop_id,
72                                    const GValue * value, GParamSpec * pspec);
73 static void gst_rtpgsmenc_get_property (GObject * object, guint prop_id,
74                                    GValue * value, GParamSpec * pspec);
75 static GstPadLinkReturn gst_rtpgsmenc_sinkconnect (GstPad * pad, GstCaps * caps);
76 static GstElementStateReturn gst_rtpgsmenc_change_state (GstElement * element);
77
78 static GstElementClass *parent_class = NULL;
79
80 static GType gst_rtpgsmenc_get_type (void)
81 {
82   static GType rtpgsmenc_type = 0;
83
84   if (!rtpgsmenc_type) {
85     static const GTypeInfo rtpgsmenc_info = {
86       sizeof (GstRtpGSMEncClass),
87       NULL,
88       NULL,
89       (GClassInitFunc) gst_rtpgsmenc_class_init,
90       NULL,
91       NULL,
92       sizeof (GstRtpGSMEnc),
93       0,
94       (GInstanceInitFunc) gst_rtpgsmenc_init,
95     };
96
97     rtpgsmenc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstRtpGSMEnc", &rtpgsmenc_info, 0);
98   }
99   return rtpgsmenc_type;
100 }
101
102 static void
103 gst_rtpgsmenc_class_init (GstRtpGSMEncClass * klass)
104 {
105   GObjectClass *gobject_class;
106   GstElementClass *gstelement_class;
107
108   gobject_class = (GObjectClass *) klass;
109   gstelement_class = (GstElementClass *) klass;
110
111   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
112
113   gobject_class->set_property = gst_rtpgsmenc_set_property;
114   gobject_class->get_property = gst_rtpgsmenc_get_property;
115
116   gstelement_class->change_state = gst_rtpgsmenc_change_state;
117 }
118
119 static void
120 gst_rtpgsmenc_init (GstRtpGSMEnc * rtpgsmenc)
121 {
122   rtpgsmenc->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_factory), "sink");
123   rtpgsmenc->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (src_factory), "src");
124   gst_element_add_pad (GST_ELEMENT (rtpgsmenc), rtpgsmenc->sinkpad);
125   gst_element_add_pad (GST_ELEMENT (rtpgsmenc), rtpgsmenc->srcpad);
126   gst_pad_set_chain_function (rtpgsmenc->sinkpad, gst_rtpgsmenc_chain);
127   gst_pad_set_link_function (rtpgsmenc->sinkpad, gst_rtpgsmenc_sinkconnect);
128
129   rtpgsmenc->frequency = 8000;
130
131   rtpgsmenc->next_time = 0; 
132   rtpgsmenc->time_interval = 0;
133
134   rtpgsmenc->seq = 0;
135   rtpgsmenc->ssrc = random ();
136 }
137
138 static GstPadLinkReturn
139 gst_rtpgsmenc_sinkconnect (GstPad * pad, GstCaps * caps)
140 {
141   GstRtpGSMEnc *rtpgsmenc;
142
143   rtpgsmenc = GST_RTP_GSM_ENC (gst_pad_get_parent (pad));
144
145   gst_caps_get_int (caps, "rate", &rtpgsmenc->frequency);
146
147   /* Pre-calculate what we can */
148   rtpgsmenc->time_interval = GST_SECOND / (2 * rtpgsmenc->frequency);
149
150   return GST_PAD_LINK_OK;
151 }
152
153
154 void
155 gst_rtpgsmenc_htons (GstBuffer *buf)
156 {
157   gint16 *i, *len;
158
159   /* FIXME: is this code correct or even sane at all? */
160   i = (gint16 *) GST_BUFFER_DATA(buf); 
161   len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
162
163   for (; i<len; i++) {
164       *i = g_htons (*i);
165   }
166 }
167
168 static void
169 gst_rtpgsmenc_chain (GstPad * pad, GstBuffer * buf)
170 {
171   GstRtpGSMEnc *rtpgsmenc;
172   GstBuffer *outbuf;
173   Rtp_Packet packet;
174
175   g_return_if_fail (pad != NULL);
176   g_return_if_fail (GST_IS_PAD (pad));
177   g_return_if_fail (buf != NULL);
178
179   rtpgsmenc = GST_RTP_GSM_ENC (GST_OBJECT_PARENT (pad));
180
181   g_return_if_fail (rtpgsmenc != NULL);
182   g_return_if_fail (GST_IS_RTP_GSM_ENC (rtpgsmenc));
183
184   if (GST_IS_EVENT (buf)) {
185     GstEvent *event = GST_EVENT (buf);
186
187     switch (GST_EVENT_TYPE (event)) {
188       case GST_EVENT_DISCONTINUOUS:
189         GST_DEBUG (GST_CAT_EVENT, "discont"); 
190         rtpgsmenc->next_time = 0;
191         gst_pad_event_default (pad, event);
192         return;
193       default:
194         gst_pad_event_default (pad, event);
195         return;
196     }
197   }
198
199   /* We only need the header */
200   packet = rtp_packet_new_allocate (0, 0, 0);
201
202   rtp_packet_set_csrc_count (packet, 0);
203   rtp_packet_set_extension (packet, 0);
204   rtp_packet_set_padding (packet, 0);
205   rtp_packet_set_version (packet, RTP_VERSION);
206   rtp_packet_set_marker (packet, 0);
207   rtp_packet_set_ssrc (packet, g_htonl (rtpgsmenc->ssrc));
208   rtp_packet_set_seq (packet, g_htons (rtpgsmenc->seq));
209   rtp_packet_set_timestamp (packet, g_htonl ((guint32) rtpgsmenc->next_time / GST_SECOND));
210   rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_GSM);
211
212   /* FIXME: According to RFC 1890, this is required, right? */
213 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
214   gst_rtpgsmenc_htons (buf);
215 #endif
216
217   outbuf = gst_buffer_new ();
218   GST_BUFFER_SIZE (outbuf) = rtp_packet_get_packet_len (packet) + GST_BUFFER_SIZE (buf);
219   GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
220   GST_BUFFER_TIMESTAMP (outbuf) = rtpgsmenc->next_time;
221
222   memcpy (GST_BUFFER_DATA (outbuf), packet->data, rtp_packet_get_packet_len (packet));
223   memcpy (GST_BUFFER_DATA (outbuf) + rtp_packet_get_packet_len(packet), GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
224
225   GST_DEBUG (0,"gst_rtpgsmenc_chain: pushing buffer of size %d", GST_BUFFER_SIZE(outbuf));
226   gst_pad_push (rtpgsmenc->srcpad, outbuf);
227
228   ++rtpgsmenc->seq;
229   rtpgsmenc->next_time += rtpgsmenc->time_interval * GST_BUFFER_SIZE (buf);
230   
231   rtp_packet_free (packet);
232   gst_buffer_unref (buf);
233 }
234
235 static void
236 gst_rtpgsmenc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
237 {
238   GstRtpGSMEnc *rtpgsmenc;
239
240   /* it's not null if we got it, but it might not be ours */
241   g_return_if_fail (GST_IS_RTP_GSM_ENC (object));
242   rtpgsmenc = GST_RTP_GSM_ENC (object);
243
244   switch (prop_id) {
245     default:
246       break;
247   }
248 }
249
250 static void
251 gst_rtpgsmenc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
252 {
253   GstRtpGSMEnc *rtpgsmenc;
254
255   /* it's not null if we got it, but it might not be ours */
256   g_return_if_fail (GST_IS_RTP_GSM_ENC (object));
257   rtpgsmenc = GST_RTP_GSM_ENC (object);
258
259   switch (prop_id) {
260     default:
261       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262       break;
263   }
264 }
265
266 static GstElementStateReturn
267 gst_rtpgsmenc_change_state (GstElement * element)
268 {
269   GstRtpGSMEnc *rtpgsmenc;
270
271   g_return_val_if_fail (GST_IS_RTP_GSM_ENC (element), GST_STATE_FAILURE);
272
273   rtpgsmenc = GST_RTP_GSM_ENC (element);
274
275   GST_DEBUG (0, "state pending %d\n", GST_STATE_PENDING (element));
276
277   /* if going down into NULL state, close the file if it's open */
278   switch (GST_STATE_TRANSITION (element)) {
279     case GST_STATE_NULL_TO_READY:
280       break;
281
282     case GST_STATE_READY_TO_NULL:
283       break;
284
285     default:
286       break;
287   }
288
289   /* if we haven't failed already, give the parent class a chance to ;-) */
290   if (GST_ELEMENT_CLASS (parent_class)->change_state)
291     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
292
293   return GST_STATE_SUCCESS;
294 }
295
296 gboolean
297 gst_rtpgsmenc_plugin_init (GModule * module, GstPlugin * plugin)
298 {
299   GstElementFactory *rtpgsmenc;
300
301   rtpgsmenc = gst_element_factory_new ("rtpgsmenc", GST_TYPE_RTP_GSM_ENC, &gst_rtpgsmenc_details);
302   g_return_val_if_fail (rtpgsmenc != NULL, FALSE);
303
304   gst_element_factory_add_pad_template (rtpgsmenc, GST_PAD_TEMPLATE_GET (sink_factory));
305   gst_element_factory_add_pad_template (rtpgsmenc, GST_PAD_TEMPLATE_GET (src_factory));
306
307   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (rtpgsmenc));
308
309   return TRUE;
310 }