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