gst-indent
[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 ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS ("audio/x-gsm, " "rate = (int) [ 1000, 48000 ]")
53     );
54
55 static GstStaticPadTemplate gst_rtpgsmenc_src_template =
56 GST_STATIC_PAD_TEMPLATE ("src",
57     GST_PAD_SRC,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp")
60     );
61
62
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);
74
75 static GstElementClass *parent_class = NULL;
76
77 static GType
78 gst_rtpgsmenc_get_type (void)
79 {
80   static GType rtpgsmenc_type = 0;
81
82   if (!rtpgsmenc_type) {
83     static const GTypeInfo rtpgsmenc_info = {
84       sizeof (GstRtpGSMEncClass),
85       (GBaseInitFunc) gst_rtpgsmenc_base_init,
86       NULL,
87       (GClassInitFunc) gst_rtpgsmenc_class_init,
88       NULL,
89       NULL,
90       sizeof (GstRtpGSMEnc),
91       0,
92       (GInstanceInitFunc) gst_rtpgsmenc_init,
93     };
94
95     rtpgsmenc_type =
96         g_type_register_static (GST_TYPE_ELEMENT, "GstRtpGSMEnc",
97         &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 =
135       gst_pad_new_from_template (gst_static_pad_template_get
136       (&gst_rtpgsmenc_sink_template), "sink");
137   rtpgsmenc->srcpad =
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);
144
145   rtpgsmenc->frequency = 8000;
146
147   rtpgsmenc->next_time = 0;
148   rtpgsmenc->time_interval = 0;
149
150   rtpgsmenc->seq = 0;
151   rtpgsmenc->ssrc = random ();
152 }
153
154 static GstPadLinkReturn
155 gst_rtpgsmenc_sinkconnect (GstPad * pad, const GstCaps * caps)
156 {
157   GstRtpGSMEnc *rtpgsmenc;
158   GstStructure *structure;
159   gboolean ret;
160
161   rtpgsmenc = GST_RTP_GSM_ENC (gst_pad_get_parent (pad));
162
163   structure = gst_caps_get_structure (caps, 0);
164
165   ret = gst_structure_get_int (structure, "rate", &rtpgsmenc->frequency);
166   if (!ret)
167     return GST_PAD_LINK_REFUSED;
168
169   /* Pre-calculate what we can */
170   rtpgsmenc->time_interval = GST_SECOND / (2 * rtpgsmenc->frequency);
171
172   return GST_PAD_LINK_OK;
173 }
174
175
176 void
177 gst_rtpgsmenc_htons (GstBuffer * buf)
178 {
179   gint16 *i, *len;
180
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 *);
184
185   for (; i < len; i++) {
186     *i = g_htons (*i);
187   }
188 }
189
190 static void
191 gst_rtpgsmenc_chain (GstPad * pad, GstData * _data)
192 {
193   GstBuffer *buf = GST_BUFFER (_data);
194   GstRtpGSMEnc *rtpgsmenc;
195   GstBuffer *outbuf;
196   Rtp_Packet packet;
197
198   g_return_if_fail (pad != NULL);
199   g_return_if_fail (GST_IS_PAD (pad));
200   g_return_if_fail (buf != NULL);
201
202   rtpgsmenc = GST_RTP_GSM_ENC (GST_OBJECT_PARENT (pad));
203
204   g_return_if_fail (rtpgsmenc != NULL);
205   g_return_if_fail (GST_IS_RTP_GSM_ENC (rtpgsmenc));
206
207   if (GST_IS_EVENT (buf)) {
208     GstEvent *event = GST_EVENT (buf);
209
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);
215         return;
216       default:
217         gst_pad_event_default (pad, event);
218         return;
219     }
220   }
221
222   /* We only need the header */
223   packet = rtp_packet_new_allocate (0, 0, 0);
224
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);
235
236   /* FIXME: According to RFC 1890, this is required, right? */
237 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
238   gst_rtpgsmenc_htons (buf);
239 #endif
240
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;
246
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));
251
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));
255
256   ++rtpgsmenc->seq;
257   rtpgsmenc->next_time += rtpgsmenc->time_interval * GST_BUFFER_SIZE (buf);
258
259   rtp_packet_free (packet);
260   gst_buffer_unref (buf);
261 }
262
263 static void
264 gst_rtpgsmenc_set_property (GObject * object, guint prop_id,
265     const GValue * value, GParamSpec * pspec)
266 {
267   GstRtpGSMEnc *rtpgsmenc;
268
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);
272
273   switch (prop_id) {
274     default:
275       break;
276   }
277 }
278
279 static void
280 gst_rtpgsmenc_get_property (GObject * object, guint prop_id, GValue * value,
281     GParamSpec * pspec)
282 {
283   GstRtpGSMEnc *rtpgsmenc;
284
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);
288
289   switch (prop_id) {
290     default:
291       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292       break;
293   }
294 }
295
296 static GstElementStateReturn
297 gst_rtpgsmenc_change_state (GstElement * element)
298 {
299   GstRtpGSMEnc *rtpgsmenc;
300
301   g_return_val_if_fail (GST_IS_RTP_GSM_ENC (element), GST_STATE_FAILURE);
302
303   rtpgsmenc = GST_RTP_GSM_ENC (element);
304
305   GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
306
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:
310       break;
311
312     case GST_STATE_READY_TO_NULL:
313       break;
314
315     default:
316       break;
317   }
318
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);
322
323   return GST_STATE_SUCCESS;
324 }
325
326 gboolean
327 gst_rtpgsmenc_plugin_init (GstPlugin * plugin)
328 {
329   return gst_element_register (plugin, "rtpgsmenc",
330       GST_RANK_NONE, GST_TYPE_RTP_GSM_ENC);
331 }