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