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