Merge CAPS branch
[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/Encoder/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 static GstStaticPadTemplate gst_rtpgsmenc_sink_template =
49 GST_STATIC_PAD_TEMPLATE (
50     "sink",
51     GST_PAD_SINK,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ( "audio/x-gsm, "
54       "rate = (int) [ 1000, 48000 ]"
55     )
56 );
57
58 static GstStaticPadTemplate gst_rtpgsmenc_src_template =
59 GST_STATIC_PAD_TEMPLATE (
60     "src",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS ("application/x-rtp")
64 );
65
66
67 static void gst_rtpgsmenc_class_init (GstRtpGSMEncClass * klass);
68 static void gst_rtpgsmenc_base_init (GstRtpGSMEncClass * klass);
69 static void gst_rtpgsmenc_init (GstRtpGSMEnc * rtpgsmenc);
70 static void gst_rtpgsmenc_chain (GstPad * pad, GstData *_data);
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, const 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       (GBaseInitFunc) gst_rtpgsmenc_base_init,
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_base_init (GstRtpGSMEncClass * klass)
104 {
105   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
106
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);
112 }
113
114 static void
115 gst_rtpgsmenc_class_init (GstRtpGSMEncClass * klass)
116 {
117   GObjectClass *gobject_class;
118   GstElementClass *gstelement_class;
119
120   gobject_class = (GObjectClass *) klass;
121   gstelement_class = (GstElementClass *) klass;
122
123   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
124
125   gobject_class->set_property = gst_rtpgsmenc_set_property;
126   gobject_class->get_property = gst_rtpgsmenc_get_property;
127
128   gstelement_class->change_state = gst_rtpgsmenc_change_state;
129 }
130
131 static void
132 gst_rtpgsmenc_init (GstRtpGSMEnc * rtpgsmenc)
133 {
134   rtpgsmenc->sinkpad = gst_pad_new_from_template (
135       gst_static_pad_template_get (&gst_rtpgsmenc_sink_template), "sink");
136   rtpgsmenc->srcpad = gst_pad_new_from_template (
137       gst_static_pad_template_get (&gst_rtpgsmenc_sink_template), "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, const GstCaps * caps)
154 {
155   GstRtpGSMEnc *rtpgsmenc;
156   GstStructure *structure;
157   gboolean ret;
158
159   rtpgsmenc = GST_RTP_GSM_ENC (gst_pad_get_parent (pad));
160
161   structure = gst_caps_get_structure (caps, 0);
162
163   ret = gst_structure_get_int (structure, "rate", &rtpgsmenc->frequency);
164   if (!ret) return GST_PAD_LINK_REFUSED;
165
166   /* Pre-calculate what we can */
167   rtpgsmenc->time_interval = GST_SECOND / (2 * rtpgsmenc->frequency);
168
169   return GST_PAD_LINK_OK;
170 }
171
172
173 void
174 gst_rtpgsmenc_htons (GstBuffer *buf)
175 {
176   gint16 *i, *len;
177
178   /* FIXME: is this code correct or even sane at all? */
179   i = (gint16 *) GST_BUFFER_DATA(buf); 
180   len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
181
182   for (; i<len; i++) {
183       *i = g_htons (*i);
184   }
185 }
186
187 static void
188 gst_rtpgsmenc_chain (GstPad * pad, GstData *_data)
189 {
190   GstBuffer *buf = GST_BUFFER (_data);
191   GstRtpGSMEnc *rtpgsmenc;
192   GstBuffer *outbuf;
193   Rtp_Packet packet;
194
195   g_return_if_fail (pad != NULL);
196   g_return_if_fail (GST_IS_PAD (pad));
197   g_return_if_fail (buf != NULL);
198
199   rtpgsmenc = GST_RTP_GSM_ENC (GST_OBJECT_PARENT (pad));
200
201   g_return_if_fail (rtpgsmenc != NULL);
202   g_return_if_fail (GST_IS_RTP_GSM_ENC (rtpgsmenc));
203
204   if (GST_IS_EVENT (buf)) {
205     GstEvent *event = GST_EVENT (buf);
206
207     switch (GST_EVENT_TYPE (event)) {
208       case GST_EVENT_DISCONTINUOUS:
209         GST_DEBUG ("discont"); 
210         rtpgsmenc->next_time = 0;
211         gst_pad_event_default (pad, event);
212         return;
213       default:
214         gst_pad_event_default (pad, event);
215         return;
216     }
217   }
218
219   /* We only need the header */
220   packet = rtp_packet_new_allocate (0, 0, 0);
221
222   rtp_packet_set_csrc_count (packet, 0);
223   rtp_packet_set_extension (packet, 0);
224   rtp_packet_set_padding (packet, 0);
225   rtp_packet_set_version (packet, RTP_VERSION);
226   rtp_packet_set_marker (packet, 0);
227   rtp_packet_set_ssrc (packet, g_htonl (rtpgsmenc->ssrc));
228   rtp_packet_set_seq (packet, g_htons (rtpgsmenc->seq));
229   rtp_packet_set_timestamp (packet, g_htonl ((guint32) rtpgsmenc->next_time / GST_SECOND));
230   rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_GSM);
231
232   /* FIXME: According to RFC 1890, this is required, right? */
233 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
234   gst_rtpgsmenc_htons (buf);
235 #endif
236
237   outbuf = gst_buffer_new ();
238   GST_BUFFER_SIZE (outbuf) = rtp_packet_get_packet_len (packet) + GST_BUFFER_SIZE (buf);
239   GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
240   GST_BUFFER_TIMESTAMP (outbuf) = rtpgsmenc->next_time;
241
242   memcpy (GST_BUFFER_DATA (outbuf), packet->data, rtp_packet_get_packet_len (packet));
243   memcpy (GST_BUFFER_DATA (outbuf) + rtp_packet_get_packet_len(packet), GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
244
245   GST_DEBUG ("gst_rtpgsmenc_chain: pushing buffer of size %d", GST_BUFFER_SIZE(outbuf));
246   gst_pad_push (rtpgsmenc->srcpad, GST_DATA (outbuf));
247
248   ++rtpgsmenc->seq;
249   rtpgsmenc->next_time += rtpgsmenc->time_interval * GST_BUFFER_SIZE (buf);
250   
251   rtp_packet_free (packet);
252   gst_buffer_unref (buf);
253 }
254
255 static void
256 gst_rtpgsmenc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
257 {
258   GstRtpGSMEnc *rtpgsmenc;
259
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);
263
264   switch (prop_id) {
265     default:
266       break;
267   }
268 }
269
270 static void
271 gst_rtpgsmenc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
272 {
273   GstRtpGSMEnc *rtpgsmenc;
274
275   /* it's not null if we got it, but it might not be ours */
276   g_return_if_fail (GST_IS_RTP_GSM_ENC (object));
277   rtpgsmenc = GST_RTP_GSM_ENC (object);
278
279   switch (prop_id) {
280     default:
281       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282       break;
283   }
284 }
285
286 static GstElementStateReturn
287 gst_rtpgsmenc_change_state (GstElement * element)
288 {
289   GstRtpGSMEnc *rtpgsmenc;
290
291   g_return_val_if_fail (GST_IS_RTP_GSM_ENC (element), GST_STATE_FAILURE);
292
293   rtpgsmenc = GST_RTP_GSM_ENC (element);
294
295   GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
296
297   /* if going down into NULL state, close the file if it's open */
298   switch (GST_STATE_TRANSITION (element)) {
299     case GST_STATE_NULL_TO_READY:
300       break;
301
302     case GST_STATE_READY_TO_NULL:
303       break;
304
305     default:
306       break;
307   }
308
309   /* if we haven't failed already, give the parent class a chance to ;-) */
310   if (GST_ELEMENT_CLASS (parent_class)->change_state)
311     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
312
313   return GST_STATE_SUCCESS;
314 }
315
316 gboolean
317 gst_rtpgsmenc_plugin_init (GstPlugin * plugin)
318 {
319   return gst_element_register (plugin, "rtpgsmenc",
320                                GST_RANK_NONE, GST_TYPE_RTP_GSM_ENC);
321 }