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