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